Iphone mpmediaplayerController, unable to restart video after user presses done
I'm having trouble with Iphone's mediaplayercontroller.
I'm able to play the video once, with no problems.
When the user presses done, I close the mediaplayer, and move my user back to the previous screen. (I'm using a navigation based application).
However, when I try to start the video again by pressing the play button, the mediaplayercontroller no longer works correctly.
All I get is a black screen. No video, no sounds.
I've already released the previous mediaplayper controller after getting the MPMoviePlayerPlaybackDidFinishNotification or the MPMoviePlayerDidExitFullscreenNotification.
Any advice would be greatly appreciated.
More Info------- Here is a snippet of my code:
- (void) viewDidAppear:(BOOL)animated
{
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayerController.view];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlaybackComplete:)
name:MPMoviePlayerDidExitFullscreenNotification
object:nil]; // This is to deal with the user pressing the done button.
[moviePlayerController setFullscreen:YES];
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
NSLog(@"movie playback ended");
int reason = [[[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
if(reason == MPMovieFinishReasonPlaybackEnded)
NSLog(@"Reason: MPMovieFinishReasonPlaybackEnded");
else if(reason == MPMovieFinishReasonPlaybackError)
NSLog(@"Reason: MPMovieFinishReasonPlaybackError");
else if(reason == MPMovieFinishReasonUserExited)
NSLog(@"Reason: MPMovieFinishReasonUserExited");
else
NSLog(@"Reason: %d", reason);
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
开发者_Python百科 object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerDidExitFullscreenNotification
object:nil];
[moviePlayerController.view removeFromSuperview];
moviePlayerController.initialPlaybackTime = -1;
[moviePlayerController pause];
[moviePlayerController stop];
[moviePlayerController release];
moviePlayerController = nil;
[[self navigationController] popViewControllerAnimated:YES];
}
-(void)videoFile:(NSString *)moviepath
{
//[tools setHidden:YES];
// bi3.enabled=NO;
// Register to receive a notification when the movie scaling mode has changed.
//NSString *moviePath = [bundle pathForResource:@"video" ofType:@"mp4"];
NSURL *movieURL1 = [[NSURL fileURLWithPath:moviepath] retain];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL1];
[theMovie setControlStyle:MPMovieControlStyleFullscreen];
[theMovie play];
MPMoviePlayerViewController *moviePlayer11 = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL1];
[self presentMoviePlayerViewControllerAnimated:moviePlayer11]; // Override point for customization after app launch
// [navigationController.view addSubview:];
//[self.view addSubview:mpMCtr.view];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification
{
bi3.enabled=YES;
[tools setHidden:NO];
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
// [player stop];
//[self.view removeFromSuperview];
[player.view removeFromSuperview];
[player autorelease];
}
- (void) moviePlayBackDidFinish:(NSNotification*)aNotification
{
MPMoviePlayerController* theMovie=[aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
//[mpMCtr stop];
[theMovie.view removeFromSuperview];
[videoTable removeFromSuperview];
//[self.navigationController popViewControllerAnimated:YES];
}
-(void)videoClick:(id)sender
{
bi3.enabled=NO;
path2 = [[NSBundle mainBundle] pathForResource:@"Interview" ofType:@"mp4" inDirectory:nil];
[self videoFile:path2];
}
精彩评论