7

在 iOS 9 MPMoviePlayer 和他的所有组件都被弃用了。我们使用 MPMoviePlayerController 通知(如MPMoviePlayerLoadStateDidChangeNotification, MPMovieDurationAvailableNotification, MPMoviePlayerPlaybackStateDidChangeNotification, MPMoviePlayerReadyForDisplayDidChangeNotification)来跟踪视频服务质量。但是现在使用 AVPlayerViewController 我找不到这些通知的正确替代品。

我现在如何替换这些通知?

4

2 回答 2

8

AVPlayerViewController它的用法与MPMoviePlayerViewController. 您使用 Key Value Observing 来确定AVPlayerAVPlayerViewController. 根据文档:

您可以使用键值观察来观察玩家的状态。为了您可以安全地添加和删除观察者,AVPlayer 序列化在调度队列上播放期间动态发生的更改通知。默认情况下,这个队列是主队列(参见 dispatch_get_main_queue)。为了确保在可能报告播放状态的动态变化时安全访问播放器的非原子属性,您必须使用接收器的通知队列序列化访问。一般情况下,这样的序列化自然是通过在主线程或者队列上调用AVPlayer的各种方法来实现的。

例如,如果您想知道播放器何时暂停rate,请在对象的属性上添加一个观察者AVPlayer

[self.player addObserver:self forKeyPath:@"rate" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context: &PlayerRateContext];

然后在观察方法中检查new值是否等于零:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if (context == &PlayerRateContext) {
        if ([[change valueForKey:@"new"] integerValue] == 0) {
            // summon Sauron here (or whatever you want to do)
        }
        return;
    }

    [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    return;
}

上面的很多属性AVPlayer都是可以观察到的。浏览Class reference

除此之外,还有几个可用于AVPlayerItem对象的通知,这些通知是有限的,但仍然很有帮助。

通知

AVPlayerItemDidPlayToEndTimeNotification

AVPlayerItemFailedToPlayToEndTimeNotification

AVPlayerItemTimeJumpedNotification

AVPlayerItemPlaybackStalledNotification

AVPlayerItemNewAccessLogEntryNotification

AVPlayerItemNewErrorLogEntryNotification

我发现AVPlayerItemDidPlayToEndTimeNotification在播放完成后从头开始寻找项目特别有用。

将这两个选项一起使用,您应该能够替换大部分(如果不是全部)通知MPMoviePlayerController

于 2015-10-02T16:27:38.733 回答
1

我查看了两者的文档,MPMoviePlayerNotificationsAVPlayerItemNotifications注意到两件事。

  1. MPMoviePlayerNotifications不要显示它们已被弃用:

    在此处输入图像描述

  2. AVPlayerItemNotifications没有我能看到的任何替代品:

    在此处输入图像描述

因此,我很困惑您所说MPMoviePlayerNotifications的已弃用,因为文档说它们可用。另外,我认为没有AVPlayerItemNotifications替代品MPMoviePlayerNotifications

于 2015-10-01T14:29:15.277 回答