MPMoviePlayerPlaybackDidFinishNotification not working when 'DONE' is pressed on movie player
I've been trying to figure this out and tried to follow any advice out there but I can't seem to get 'MPMoviePlayerPlaybackDidFinishNotification' to work after the user presses 'Done' on the movie player.
- (void)myMovieFinishedCallback:(NSNotification*)aNotification {
MPMoviePlayerController* theMovie=[aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];
[theMovie pause];
[theMovie stop];
[theMovie autorelease];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
}
- (void)myMovieViewFinishedCallback:(NSNotification*)aNotification {
MPMoviePlayerViewController* theMovieView=[aNotification object];
[self dismissMoviePlayerViewControllerAnimated];
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovieView];
[theMovieView pause];
[theMovieView stop];
[theMovieView autorelease];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
}
- (IBAction)safetyVideo:(id)sender {
NSString *path = [[NSBundle mainBundle] pathForResource:@"Ball_Crunch" ofType:@"m4v"];
if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 3.2) {
MPMoviePlayerViewController*tmpMoviePlayViewController=[[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fi开发者_Go百科leURLWithPath:path]] retain];
if (tmpMoviePlayViewController) {
[self presentMoviePlayerViewControllerAnimated:tmpMoviePlayViewController];
tmpMoviePlayViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieViewFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:tmpMoviePlayViewController];
[tmpMoviePlayViewController.moviePlayer play];
}
}else{
MPMoviePlayerController* theMovie = [[[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]] retain];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];
[theMovie play];
}
}
The movie plays fine and disappears when 'Done' is pressed but the callbacks are never called. Any suggestions?
Thanks.
Same problem here. Figured out, when I send nil
as object (instead of the MoviePlayerController
) the callback is triggered...
I had the same problem. This post saved me. If the video goes full screen, capture MPMoviePlayerDidExitFullscreenNotification
instead of MPMoviePlayerPlaybackDidFinishNotification
. I capture both below just in case I change my mind later.
- (void)videoButtonClick:(id)sender {
MPMoviePlayerController* moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:theVideoURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayerController];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.shouldAutoplay = YES;
moviePlayerController.initialPlaybackTime = 0;
moviePlayerController.scalingMode = MPMovieScalingModeAspectFit;
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
- (void)moviePlayBackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController* moviePlayerController = notification.object;
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayerController];
moviePlayerController.initialPlaybackTime = -1;
[moviePlayerController stop];
[moviePlayerController release];
}
Make sure for
moviePlayer.repeatMode = MPMovieRepeatModeNone;
i realize this is a old question, but none of the above solved my problems. If you end up trying everything like i did, make sure your parent view controller aint using the same method for notification. I was sending the notification to the wrong place.
Change the selector name for something less usual and try again.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MyMovieClosedThisTimeForReal:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
You can observe to UIWindowDidBecomeVisibleNotification
and UIWindowDidBecomeHiddenNotification
as stated in this post. This works even in iOS 8.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowVisible:)
name:UIWindowDidBecomeVisibleNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowHidden:)
name:UIWindowDidBecomeHiddenNotification
object:self.view.window];
- (void)windowVisible:(NSNotification *)notification
{
NSLog(@"-windowVisible");
}
- (void)windowHidden:(NSNotification *)notification
{
NSLog(@"-windowHidden");
}
I also had this problem and no other solution worked for me. The problem is that when adding the observer, the object parameter should be tmpMoviePlayViewController.moviePlayer and not just moviePlayer.
The tmpMoviePlayViewController.moviePlayer (class MPMoviePlayerController) is the one sending the notification, not the tmpMoviePlayViewController (class MPMoviePlayerViewController).
So change this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myMovieViewFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification object:tmpMoviePlayViewController];
to this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myMovieViewFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification object:tmpMoviePlayViewController.moviePlayer];
精彩评论