4

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

以下是结束原因键:

  • MPMovieFinishReasonPlaybackEnded
  • MPMovieFinishReasonPlaybackError
  • MPMovieFinishReasonUserExited
4

2 回答 2

5

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
        }
    }
}
于 2015-10-20T13:58:57.987 回答
2

尝试,在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
}
于 2015-10-20T13:58:18.563 回答