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!