A problem with Media Player base on iOS4 and deploy iOS3 - iphone

when Run on Device 3.1.2, why it also pass if(NSClassFromString(#"MPMoviePlayerViewController") != nil)
and do code of iOS4 then it will crash , how to fix this issues?
if(NSClassFromString(#"MPMoviePlayerViewController") != nil) {
// iOS 4 code
NSLog(#"MPMoviePlayerViewController");
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:AppDelegate.PushLink]];
if (mp) {
// save the movie player object
self.theMovie4 = mp;
[mp release];
//Present
[self presentMoviePlayerViewControllerAnimated:self.theMovie4];
// Play the movie!
self.theMovie4.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[self.theMovie4.moviePlayer play];
}
}
else {
//iOS 3 Code
AppDelegate = nil;
AppDelegate = [[UIApplication sharedApplication] delegate];
[AppDelegate ForceHideNavigationBar];
theMovie3 = nil;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePreloadDidFinish:)
name:MPMoviePlayerContentPreloadDidFinishNotification
object:theMovie3];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie3];
// Register to receive a notification when the movie scaling mode has changed.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieScalingModeDidChange:)
name:MPMoviePlayerScalingModeDidChangeNotification
object:theMovie3];
theMovie3 = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:AppDelegate.PushLink]];
[theMovie3 play];
}

I am doing a very similar thing on my app which is using 4.0 as the base SDK and yet I am targeting iOS3 devices too. I had to check the OS version to properly provide the right code for the video playback. For example:
- (void) playMovieAtURL: (NSURL*) theURL {
NSLog(#"playMovieAtURL");
if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 3.2) {
NSLog(#"> 3.2");
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:theURL];
if (mp)
{
// save the movie player object
//self.moviePlayerViewController = mp;
//[mp release];
[self presentMoviePlayerViewControllerAnimated:mp];
mp.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[mp.moviePlayer play];
[mp release];
}
}
else if ([[[UIDevice currentDevice] systemVersion] doubleValue] < 3.2) {
NSLog(#"< 3.2");
MPMoviePlayerController* theMovie = [[MPMoviePlayerController alloc] initWithContentURL: theURL];
theMovie.scalingMode = MPMovieScalingModeAspectFill;
// Register for the playback finished notification
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: #selector(myMovieFinishedCallback:)
name: MPMoviePlayerPlaybackDidFinishNotification
object: theMovie];
// Movie playback is asynchronous, so this method returns immediately.
[theMovie play];
}
}
Hope this helps!!

[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
NSString *movpath = [[NSBundle mainBundle] pathForResource:#"splash" ofType:#"mp4"];
MPMoviePlayerViewController* mpviewController = [[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:movpath]];
[window addSubview:mpviewController.view];
[window makeKeyAndVisible];
MPMoviePlayerController *mp = [mpviewController moviePlayer];
[mp prepareToPlay];
[[mpviewController moviePlayer] play];
This code will work in iOS4
It however does quit once the movie completes because you must create the method for the notification. Also you need to release the movie there so that you can catch the memory leak.

Related

MPMoviePlayerController doesnot play the video its full length

I am using the MPMoviePlayerController to open a video file.The video runs fine.But suppose if the video file is 10 sec ,with 1 sec remaining the video stops.Is that its natural way of playing or should we specify something.The follow is the code used
NSURL *fileURL = [NSURL URLWithString:location];
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
self.moviePlayerController.controlStyle = MPMovieControlStyleFullscreen;
self.moviePlayerController.movieSourceType = MPMovieSourceTypeFile;
[self.moviePlayerController prepareToPlay];
[self.moviePlayerController.view setFrame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview:self.moviePlayerController.view];
- (void)moviePlaybackComplete:(NSNotification *)notification {
NSNumber *finishReason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
if ([finishReason intValue] != MPMovieFinishReasonPlaybackEnded) {
self.moviePlayerController = [notification object];
self.moviePlayerController.view.hidden = YES;
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[self.moviePlayerController.view removeFromSuperview];
[self.moviePlayerController release];
}
[self dismissViewControllerAnimated:YES completion:nil];
}

MPMoviePlayer controlStyle

I want to hide the controls from the MPMoviePlayer with this code:
-(IBAction)video:(id)sender {
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"Intro" ofType:#"mov"];
NSURL *movie = [NSURL fileURLWithPath:moviePath];
MPMoviePlayerController *control = [[MPMoviePlayerController alloc]initWithContentURL:movie];
//[self.view addSubview: control.view];
control.scalingMode = MPMovieScalingModeFill;
control.controlStyle = MPMovieControlStyleNone;
control.shouldAutoplay = YES;
[control play];
MPMoviePlayerViewController *movieplayer = [[MPMoviePlayerViewController alloc]initWithContentURL:movie];
[self presentMoviePlayerViewControllerAnimated:movieplayer]; }
But that does not work.
You are repeating code. MPMoviePlayerViewController has MPMoviePlayerController. So use it as movieplayervc.moviePlayer.controlStyle = MPMovieControlStyleNone;
have you tried this
[videoPlayerobj setControlStyle:MPMovieControlStyleNone];
My player is set up in the viewDidLoad and this line hides the MPMoviePlayerController. I have intialised my MPMoviePlayer controller as *stream.
stream.view.hidden = YES;
Hope this helps!
You can play video and stop video and remove from your custom view with this code. and MPMoviePlayerController is movie player.
Hope this is useful for you.thank you
-(void)playMovie:(id)sender {
UIButton *buttonThatWasPressed = (UIButton *)sender;
buttonThatWasPressed.enabled = NO;
NSString * str=[[NSBundle mainBundle]pathForResource:#"yo2" ofType:#"mov"];
NSURL * url=[NSURL fileURLWithPath:str];
MPMoviePlayerController * movieController=[[MPMoviePlayerController alloc]initWithContentURL:url];
movieController.controlStyle=MPMovieControlStyleFullscreen;
[movieController.view setFrame:self.view.bounds];
[self.view addSubview:movieController.view];
[movieController prepareToPlay];
[movieController play];
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDonePressed:)
name:MPMoviePlayerDidExitFullscreenNotification
object:_moviePlayer];
_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES]; }
This method is called when your video or movie is stop from user or video playback has finish.
-(void) moviePlayBackDonePressed:(NSNotification*)notification {
[_moviePlayer stop];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:_moviePlayer];
if ([_moviePlayer respondsToSelector:#selector(setFullscreen:animated:)])
{
[_moviePlayer.view removeFromSuperview];
}
_moviePlayer=nil;
[self dismissViewControllerAnimated:YES
completion:^{
[self performSegueWithIdentifier:#"show" sender:self];
}];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification { // Remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[self dismissViewControllerAnimated:YES
completion:^{
[self performSegueWithIdentifier:#"show" sender:self];
}];
}

MPMoviePlayerViewController quits while playing the video

I am recording the video in my application. I am saving that video inside Documents directory. After taking the video if I try to play the video using the following code. The player opens and quits in one seconds. However if I Quit and then open my app fresh the video I took earlier is playing.
+ (void) playMovieAtURL: (NSURL*) theURL :(id)sender{
NSLog(#"playMovieAtURL");
//senderID = (id *)sender;
if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 3.2) {
NSLog(#"> 3.2");
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:theURL];
if (mp)
{
// save the movie player object
//self.moviePlayerViewController = mp;
//[mp release];
[sender presentMoviePlayerViewControllerAnimated:mp];
//mp.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
mp.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[mp.moviePlayer play];
}
[mp release];
}
else if ([[[UIDevice currentDevice] systemVersion] doubleValue] < 3.2) {
NSLog(#"< 3.2");
MPMoviePlayerController* theMovie = [[MPMoviePlayerController alloc] initWithContentURL: theURL];
theMovie.scalingMode = MPMovieScalingModeAspectFill;
// Register for the playback finished notification
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: #selector(myMovieFinishedCallback:)
name: MPMoviePlayerPlaybackDidFinishNotification
object: theMovie];
// Movie playback is asynchronous, so this method returns immediately.
[theMovie play];
[theMovie release];
}
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *aMoviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:aMoviePlayer];
// If the moviePlayer.view was added to the view, it needs to be removed
if ([aMoviePlayer respondsToSelector:#selector(setFullscreen:animated:)]) {
[aMoviePlayer.view removeFromSuperview];
}
}
This is the code I use for storing the video inside documents.
NSURL *videoURL = [imageInfo objectForKey:UIImagePickerControllerMediaURL];
NSData *webData = [NSData dataWithContentsOfURL:videoURL];
self.itsVideoName = fileName;
[webData writeToFile:[NSString stringWithFormat:#"%#/%#",dataPath,fileName] atomically:TRUE];
I finally figured out the answer after few hours of searching. This is the code I am using now..
-(IBAction)playMovie:(NSString *) theURL
{
NSURL *fileURL = [NSURL fileURLWithPath:theURL];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
//After putting the following line the problem solved.
moviePlayerController.useApplicationAudioSession = NO;
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}
the code moviePlayerController.useApplicationAudioSession = NO; is used to solve this problem. I am not sure how this problem solved I guess I am using session for audio I guess that was causing problem to this.
Also the code which I originally posted was taking lots of memory like if I opened 2 video it cost 104 mb allocation for me. This new code was perfect and it is taking only less memory. Hope this will help for some one..
*NSURL fileURL = [NSURL fileURLWithPath:theURL];
should use NSURL *fileURL = [NSURL URLWithString:theURL];

How to change view after video played?

I would like to make an application in which, when I press a button, one video starts playing, and when it finishes or when I press "done" button it should take me to a view different from the first one where I launched the video.
Update:
This is the code I'm using but it doesn't work. I need to use MPMoviePlayerViewController instead of MPMoviePlayerController. Any idea?
NSBundle *Bundle = [NSBundle mainBundle];
NSString *moviePath = [Bundle pathForResource:#"video1" ofType:#"m4v"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
- (void) movieFinishedCallback:(NSNotification*) notification {
MPMoviePlayerViewController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer release];
// dismiss your view or present a new view here.
View1 *View1b = [[View1 alloc] initWithNibName:nil bundle:nil];
View1b.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController: View1b animated:YES];
}
Unless you need something very custom, MPMoviePlayerController will probably suit your needs. You can add it to your view or a smaller subview, and you can disable the controls for fullscreen, etc. The url can be to a local file or remove resource.
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: url];
[player view].frame = [myView bounds];
[myView addSubview: [player view]];
[player play];
Observe the MPMoviePlayerPlaybackDidFinishNotification to figure out when it is done, and from that observer's block or selected method you can dismiss your view or present a new view.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
Then
- (void) movieFinishedCallback:(NSNotification*) notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player release];
// dismiss your view or present a new view here.
}

Error Message: MPMoviePlayerController instance is already playing

In my app I am playing video(s) that are in the app, using the standard MPMoviePlayerController class.
The first time around around this works great, however after watching 1 video if you try and watch something else the app crashes on MPMoviePlayerController's play method with the error:
*** Terminating app due to uncaught exception 'MPMoviePlayerControllerPlaybackException', reason: 'MPMoviePlayerController instance is already playing'
I can not figure out why this is happening.
I have VERY similar code in another app and I don't get this error.
I am compiling for the device - 2.0 and running it on an iPhone with firmware 2.2.1.
This is the code I have:
#synthesize movieURL;
- (void) setMovieAndPlay
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
moviePath = [[[paths lastObject] stringByAppendingPathComponent:movieURL] retain];
[self playVideoWithURL:[NSURL fileURLWithPath:moviePath]];
}
-(void)playMovieAtURL:(NSURL*)theURL
{
MPMoviePlayerController *mMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theURL];
mMoviePlayer.scalingMode = MPMovieScalingModeAspectFill;
if ([defaults boolForKey:#"disableControls"])
{
mMoviePlayer.movieControlMode = MPMovieControlModeHidden;
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:mMoviePlayer];
[mMoviePlayer play];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *theMovie = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];
[theMovie release];
theMovie = nil;
NSDictionary *notiUserInfo = [notification userInfo];
if (notiUserInfo != nil)
{
NSError *errorInfo = [notiUserInfo objectForKey:#"error"];
if ([[errorInfo domain] isEqualToString:#"MediaPlayerErrorDomain"])
{
UIAlertView *notice = [[UIAlertView alloc] initWithTitle:#"Error" message:[errorInfo localizedDescription] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[notice show];
[notice release];
return;
}
}
if ([defaults boolForKey:#"autoRepeat"])
{
[self playMovieAtURL:[NSURL fileURLWithPath:moviePath]];
}
else
{
KFAppDelegate *appDelegate = (KFAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate endMovie];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
KFAppDelegate *appDelegate = (KFAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate endMovie];
}
What is even stranger is, if you look at the code, after the movie ends I check if the user has enable auto-repeat.
If they have, I just start the movie over again, and THIS WORKS.
However if they did not enable auto-repeat and leave this class and then try to watch another movie (or the same one) it gives that crash.
Does anyone know why this would be happening?
Am I doing something wrong?
Thanks!
Here's my solution. I had to make the next playback wait 1 second using NSTimer or I'd get the same audio-only problem everyone has been talking about. This is for 2.2.1.
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(playTrack) userInfo:nil repeats:NO];
}
- (void) playTrack {
[moviePlayer stop];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
[moviePlayer release];
NSURL *nsUrl = [NSURL URLWithString:track.url];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:nsUrl];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer play];
}
I think in
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
// you should add this:
if (mMoviePlayer != nil) {
// free the old movie player
NSLog(#"releasing!");
[mMoviePlayer release];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
}
You will get this error only if your MPMoviePlayerController object has not been released and you are trying to play another movie.
Create an instance variable of MPMoviePlayerController and use that through out your code. So instead of
theMovie = [notification object];
[theMovie release];
theMovie = nil;
use an instance variable and use that in playMovieAtURL and playbackDidFinish methods. My problem was that the movie player object was not getting released and so I could not see the next video, only hear the audio playing.
Hopefully that'll help.
This seems to have been fixed in newer iOS versions.