如何替换MPMoviePlayer通知?

Rom*_*uba 7 mpmovieplayercontroller ios avplayer ios9 avplayerviewcontroller

在iOS 9中,MPMoviePlayer和他的所有组件都已弃用.我们使用MPMoviePlayerController通知MPMoviePlayerLoadStateDidChangeNotification, MPMovieDurationAvailableNotification, MPMoviePlayerPlaybackStateDidChangeNotification, MPMoviePlayerReadyForDisplayDidChangeNotification来跟踪视频服务质量.但是现在使用AVPlayerViewController我找不到这些通知的正确替换.

如何立即替换这些通知?

Ish*_*nda 8

AVPlayerViewController它的用法与它有很大的不同MPMoviePlayerViewController.您可以使用键值观察来确定AVPlayer与其关联的对象的当前特征,而不是使用通知AVPlayerViewController.根据文件:

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

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

[self.player addObserver:self forKeyPath:@"rate" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context: &PlayerRateContext];
Run Code Online (Sandbox Code Playgroud)

然后在observe方法中检查该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;
}
Run Code Online (Sandbox Code Playgroud)

很多属性AVPlayer都是可以观察到的.浏览Class引用.

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

通知

AVPlayerItemDidPlayToEndTimeNotification

AVPlayerItemFailedToPlayToEndTimeNotification

AVPlayerItemTimeJumpedNotification

AVPlayerItemPlaybackStalledNotification

AVPlayerItemNewAccessLogEntryNotification

AVPlayerItemNewErrorLogEntryNotification

AVPlayerItemDidPlayToEndTimeNotification一旦播放完成,我发现在项目开始时寻找特别有用.

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