既然不推荐使用MPMoviePlayerPlaybackDidFinishReasonUserInfoKey,那么找到回放结束原因的最佳方法是什么?

时间:2015-10-20 13:34:08

标签: ios mpmovieplayercontroller avplayerviewcontroller

在iOS9中,MPMoviePlayer类已全部弃用,转而支持AVPlayer。我有一个使用MPMoviePlayerPlaybackDidFinishReasonUserInfoKey的现有应用程序来确定如何记录视频播放器结束的事件。我如何对AVPlayer做同样的事情?

以下是结束原因键:

  • MPMovieFinishReasonPlaybackEnded
  • MPMovieFinishReasonPlaybackError
  • MPMovieFinishReasonUserExited

2 个答案:

答案 0 :(得分:4)

AVKit中没有与MPMoviePlayerPlaybackDidFinishReasonUserInfoKey和MPMoviePlayerPlaybackDidFinishNotification等效的内容。要在AVKit中完成相同的功能,您必须单独收听三个通知,而不是一个通知,原因可能不同。

  • MPMovieFinishReasonPlaybackEnded>>> AVPlayerItemDidPlayToEndTimeNotification
  • MPMovieFinishReasonPlaybackError>>> AVPlayerItemFailedToPlayToEndTimeNotification
  • MPMovieFinishReasonUserExited。没有转换。有多种方法可以检测用户是否杀死了播放器。一个是检测模态已关闭。

如果您想知道视频是否正在播放,您可以进行KVO:

[self.player addObserver:self forKeyPath:@"rate" options:0 context:nil];

然后添加此方法:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{
    if ([keyPath isEqualToString:@"rate"]) {
        if ([self.player rate]) {
            [self changeToPause];  // This changes the button to Pause
        }
        else {
            [self changeToPlay];   // This changes the button to Play
        }
    }
}

答案 1 :(得分:1)

尝试viewDidLoad

    AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:@"yoururl"]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];

    AVPlayer* player = [[[AVPlayer alloc] initWithPlayerItem:playerItem] autorelease];

    [player play]

-(void)itemDidFinishPlaying:(NSNotification *) notification {
    // Will be called when AVPlayer finishes playing playerItem
}