【问题标题】:Done button event MPMoviePlayerController完成按钮事件 MPMoviePlayerController
【发布时间】:2012-01-03 21:29:53
【问题描述】:

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

效果很好!但问题是当我在视频/音频文件仍在播放时按下“完成”按钮。那么这个方法就不会被触发了……

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

【问题讨论】:

    标签: ios objective-c mpmovieplayercontroller


    【解决方案1】:

    当我在 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
        }
    }
    

    【讨论】:

    • 我的 MPMovieplayer 风格是全屏。现在点击完成按钮 - 视频只是暂停,它不会通知 MPMoviePlayerWillExitFullscreenNotification。有什么想法吗?
    • 值得注意的是,我必须在 viewController 中创建一个属性才能使其正常工作...MPMoviePlayerController *moviePlayer = 不起作用。
    • @Rajneesh071 改用MPMoviePlayerPlaybackDidFinishNotification。 @Magoo 它可以在没有属性的情况下工作(在 iOS 8.3 上测试)
    【解决方案2】:

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

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

    所选答案已整合我的回复。请参考以上内容。

    【讨论】:

    • 观察者是:MPMoviePlayerPlaybackDidFinishNotification
    【解决方案3】:

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

    大功告成!!!

    【讨论】:

    • 谢谢!将 [self dismissModalViewControllerAnimated:YES]; 更改为 [self dismissViewControllerAnimated:YES completion:nil]; 以避免在 iOS 8 上出现弃用警告。
    • 找出它不工作的原因。您需要将 removeObserver:playerVC 更改为 removeObserver:self
    【解决方案4】:
    @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");
        }
    }
    

    【讨论】:

    • 这里的问题是,如果用户暂停视频然后点击“最小化”按钮,doneButtonClick: 逻辑是错误的。如果视频处于暂停状态,有没有人想出一种方法来区分“完成”和“最小化”?
    【解决方案5】:

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

    【讨论】:

      【解决方案6】:

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

          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.

      【讨论】:

        【解决方案7】:

        Apple 在这件事上的文档很差。它建议收听 MPMoviePlayerDidExitFullscreenNotification(或 WillExit...)而不是 MPMoviePlayerDidFinishNotification,因为当用户点击完成时它不会触发。这完全不是真的!我刚刚使用 iPad Simulator (iOS 7.1 + 8.0) 在 Xcode 6.0 上对其进行了测试,当点击 DONE 时仅触发 MPMoviePlayerDidFinishNotification。

        我要向 user523234 致意,他在上面的其中一个 cmets 上做对了。

        注册以下观察者

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-29
          • 2013-09-30
          • 2016-11-28
          • 1970-01-01
          • 2017-01-24
          • 2017-04-17
          相关资源
          最近更新 更多