完成button事件MPMoviePlayerController

在我的iPhone上,我以全屏模式播放video/audio文件。 当video/audio文件到达结尾时,将触发以下方法:

- (void) movieFinishedCallback:(NSNotification*) aNotification { MPMoviePlayerController *player = [aNotification object]; [player stop]; [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player]; [player autorelease]; [moviePlayer.view removeFromSuperview]; NSLog(@"stopped?"); } 

这工作正常! 但问题是,当我按下“完成”button时,video/audio文件仍在播放。 那么这个方法不会被触发

任何人都知道如何按下“完成”button时捕捉事件? 因为现在媒体播放器停留在视图中。 它不消失。

它在iPad上工作,当我听MPMoviePlayerWillExitFullscreenNotification

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doneButtonClick:) name:MPMoviePlayerWillExitFullscreenNotification object:nil]; 

和select器方法:

 -(void)doneButtonClick:(NSNotification*)aNotification{ NSNumber *reason = [notification.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]; if ([reason intValue] == MPMovieFinishReasonUserExited) { // Your done button action here } } 

检查通知用户信息字典中的枚举

 NSNumber *reason = [notification.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]; if ([reason intValue] == MPMovieFinishReasonUserExited) { // done button clicked! } 

选定的答案已经整合了我的回应。 请参阅上文。

成功在iOS7和iOS8中进行testing

检查并删除先前的通知观察者(如果有的话) MPMoviePlayerPlaybackDidFinishNotification

 - (void)playVideo:(NSString*)filePath { // Pass your file path NSURL *vedioURL =[NSURL fileURLWithPath:filePath]; MPMoviePlayerViewController *playerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:vedioURL]; // Remove the movie player view controller from the "playback did finish" notification observers [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:playerVC.moviePlayer]; // Register this class as an observer instead [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:playerVC.moviePlayer]; // Set the modal transition style of your choice playerVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; // Present the movie player view controller [self presentViewController:playerVC animated:YES completion:nil]; // Start playback [playerVC.moviePlayer prepareToPlay]; [playerVC.moviePlayer play]; } 

closures控制器

 - (void)movieFinishedCallback:(NSNotification*)aNotification { // Obtain the reason why the movie playback finished NSNumber *finishReason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]; // Dismiss the view controller ONLY when the reason is not "playback ended" if ([finishReason intValue] != MPMovieFinishReasonPlaybackEnded) { MPMoviePlayerController *moviePlayer = [aNotification object]; // Remove this class from the observers [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; // Dismiss the view controller [self dismissViewControllerAnimated:YES completion:nil]; } } 

你完成了!

 @property (nonatomic, strong) MPMoviePlayerController *moviePlayer; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doneButtonClick:) name:MPMoviePlayerDidExitFullscreenNotification object:nil]; - (void)doneButtonClick:(NSNotification*)aNotification { if (self.moviePlayer.playbackState == MPMoviePlaybackStatePaused) { NSLog(@"done button tapped"); } else { NSLog(@"minimize tapped"); } } 

Swift版本,对于任何感兴趣的人:

 NSNotificationCenter.defaultCenter().addObserver(self, selector: "moviePlayerDoneButtonClicked:", name: MPMoviePlayerPlaybackDidFinishNotification, object: nil) 

通知处理程序:

 func moviePlayerDoneButtonClicked(note: NSNotification) { let reason = note.userInfo?[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] if (MPMovieFinishReason(rawValue: reason as! Int) == MPMovieFinishReason.UserExited) { self.exitVideo() } } 

哇,这么多错误的方法。 答案很简单:

  moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]]; [self.navigationController presentMoviePlayerViewControllerAnimated:moviePlayerViewController]; 

如果您有一分钟,请检查此链接: https : //developer.apple.com/library/prerelease/ios/documentation/MediaPlayer/Reference/UIViewController_MediaPlayer_Additions/index.html

快乐的编码! Z.

苹果的文档在这个问题上很差。 它build议侦听MPMoviePlayerDidExitFullscreenNotification(或WillExit …),而不是用于MPMoviePlayerDidFinishNotification,因为当用户点击完成时它不会触发。 这完全不是真的! 我刚刚用iPad Simulator(iOS 7.1 + 8.0)在Xcode 6.0上进行了testing,当点击DONE时,只会触发MPMoviePlayerDidFinishNotification。

我对上述评论之一的用户523234表示赞许。

注册以下观察员

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackStateChanged:) name:MPMoviePlayerPlaybackDidFinishNotification object:_mpc];