ios 完成按钮事件 MPMoviePlayerController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8227801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:31:48  来源:igfitidea点击:

Done button event MPMoviePlayerController

iosobjective-cmpmovieplayercontroller

提问by w00

On my iPhone i'm playing video/audio files in fullscreen mode. When the video/audio file reached its end then the following method is triggered:

在我的 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?");
}

That works fine! But the problem is when i press on the "Done" button when the video/audio file is still playing. Then this method doesn't get triggered...

这很好用!但问题是当我在视频/音频文件仍在播放时按下“完成”按钮。然后这个方法不会被触发......

Anyone any idea how to catch the event when the "Done" button is pressed? Because right now media player stays in the view. Its not disappearing.

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

回答by beryllium

It worked for me on iPad when I listen to MPMoviePlayerWillExitFullscreenNotification.

当我在 iPad 上听 MPMoviePlayerWillExitFullscreenNotification.

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

And selector method:

和选择器方法:

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

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

回答by dklt

check for a enum inside notification userInfo dictionary

检查通知 userInfo 字典中的枚举

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

if ([reason intValue] == MPMovieFinishReasonUserExited) {

   // done button clicked!

}

Selected answer has since integrated my response. Please refer above.

选定的答案已经整合了我的回答。请参考以上。

回答by Kampai

SUCCESSFULLY TESTED IN iOS7 AND iOS8

在 iOS7 和 iOS8 中成功测试

Check and remove earlier notification observer if any for MPMoviePlayerPlaybackDidFinishNotification.

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

Dismiss controller

解除控制器

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

You'r Done !!!

你完成了!!!

回答by iCoder

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

回答by Lirik

Swift version, for anyone who's interested:

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

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

Notification handler:

通知处理程序:

func moviePlayerDoneButtonClicked(note: NSNotification) {

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

回答by Zoltán

Wow, so many wrong approaches. The answer is this simple:

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

    moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]];

    [self.navigationController presentMoviePlayerViewControllerAnimated:moviePlayerViewController];

Check this link if you have a minute: https://developer.apple.com/library/prerelease/ios/documentation/MediaPlayer/Reference/UIViewController_MediaPlayer_Additions/index.html

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

Happy coding! Z.

快乐编码!Z。

回答by nstein

Apple's documentation is very poor on this matter. It suggests to listen for MPMoviePlayerDidExitFullscreenNotification (or WillExit...) and NOT for MPMoviePlayerDidFinishNotification as it doesn't fire when the user taps Done. This is entirely not true! I've just tested it on Xcode 6.0 with iPad Simulator (iOS 7.1 + 8.0) and only MPMoviePlayerDidFinishNotification is fired when DONE is tapped.

苹果在这方面的文档非常糟糕。它建议监听 MPMoviePlayerDidExitFullscreenNotification(或 WillExit...)而不是 MPMoviePlayerDidFinishNotification,因为当用户点击 Done 时它​​不会触发。这完全不是真的!我刚刚在带有 iPad Simulator (iOS 7.1 + 8.0) 的 Xcode 6.0 上对其进行了测试,并且在点击 DONE 时仅触发 MPMoviePlayerDidFinishNotification。

My compliments to user523234 who got it right on one of the comments above.

我向 user523234 表示赞赏,他在上述评论之一中做得对。

Register the following observer

注册以下观察者

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