完成按钮事件MPMoviePlayerController

时间:2011-11-22 13:43:52

标签: ios objective-c mpmovieplayercontroller

在我的iPhone上,我以全屏模式播放视频/音频文件。当视频/音频文件到达终点时,将触发以下方法:

- (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?");
}

工作正常!但问题是当我按下" Done"视频/音频文件仍在播放时按钮。那么这种方法不会被触发......

任何人都知道如何在"完成"按下按钮?因为现在媒体播放器仍然在视图中。它并没有消失。

7 个答案:

答案 0 :(得分:44)

当我听的时候,它在iPad上适用于我 MPMoviePlayerWillExitFullscreenNotification

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

选择器方法:

-(void)doneButtonClick:(NSNotification*)aNotification{
    NSNumber *reason = [notification.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];

    if ([reason intValue] == MPMovieFinishReasonUserExited) {
        // Your done button action here
    }
}

答案 1 :(得分:21)

检查通知userInfo字典中的枚举

NSNumber *reason = [notification.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];

if ([reason intValue] == MPMovieFinishReasonUserExited) {

   // done button clicked!

}

已选择的答案已整合我的回复。请参考上文。

答案 2 :(得分:17)

在iOS7和iOS8中成功测试

检查并删除先前的通知观察者(如果有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];
}

关闭控制器

- (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];
    }
}

你完成了!!!

答案 3 :(得分:2)

@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");
    }
}

答案 4 :(得分:2)

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()
    }
}

答案 5 :(得分:1)

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

    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

快乐的编码! ž。

答案 6 :(得分:0)

Apple的文档在这件事上非常糟糕。它建议监听MPMoviePlayerDidExitFullscreenNotification(或WillExit ...)而不是MPMoviePlayerDidFinishNotification,因为它不会在用户点击Done时触发。这完全不是真的!我刚刚在Xcode 6.0上使用iPad模拟器(iOS 7.1 + 8.0)对其进行了测试,并且在点击DONE时仅触发了MPMoviePlayerDidFinishNotification。

我对用户523234表示赞赏,他们在上述评论之一上做得对。

注册以下观察员

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