如何替换 MPMoviePlayer 通知?
Posted
技术标签:
【中文标题】如何替换 MPMoviePlayer 通知?【英文标题】:How do I replace MPMoviePlayer notifications? 【发布时间】:2015-09-29 13:28:54 【问题描述】:在 ios 9 MPMoviePlayer 和他的所有组件都已弃用。
我们使用 MPMoviePlayerController 通知(如 MPMoviePlayerLoadStateDidChangeNotification, MPMovieDurationAvailableNotification, MPMoviePlayerPlaybackStateDidChangeNotification, MPMoviePlayerReadyForDisplayDidChangeNotification
)来跟踪视频服务质量。但是现在使用 AVPlayerViewController 我找不到这些通知的正确替代品。
我现在如何替换这些通知?
【问题讨论】:
【参考方案1】:AVPlayerViewController
的用法与MPMoviePlayerViewController
有很大不同。您不使用通知,而是使用键值观察来确定与AVPlayerViewController
关联的AVPlayer
对象的当前特征。根据文档:
您可以使用键值观察来观察玩家的状态。所以 您可以安全地添加和删除观察者,AVPlayer 序列化 在播放过程中动态发生的更改通知 调度队列。默认情况下,这个队列是主队列(见 dispatch_get_main_queue)。确保安全访问玩家的 非原子属性,而播放状态的动态变化可能是 报告,您必须使用接收者的通知序列化访问 队列。在常见的情况下,这样的序列化自然是通过 在主线程或队列上调用 AVPlayer 的各种方法。
例如,如果您想知道播放器何时暂停,请在 AVPlayer
对象的 rate
属性上添加观察者:
[self.player addObserver:self forKeyPath:@"rate" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context: &PlayerRateContext];
然后在观察方法中检查new
的值是否等于0:
- (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
的通知
【讨论】:
能否提供一份 JWPlayer 通知列表?在网上找不到简明的清单。 如何在 AVPlayer 中转换 MPMoviePlayerPlaybackStateDidChangeNotification?【参考方案2】:我查看了MPMoviePlayerNotifications
和AVPlayerItemNotifications
的文档,发现了两件事。
MPMoviePlayerNotifications
不要表明它们已被弃用:
AVPlayerItemNotifications
没有我能看到的替代品:
所以,我很困惑您说 MPMoviePlayerNotifications
已弃用,因为文档说它们可用。另外,我不认为AVPlayerItemNotifications
可以替代MPMoviePlayerNotifications
。
【讨论】:
来自文档:The MPMoviePlayerViewController class is formally deprecated in iOS 9. (The MPMoviePlayerController class is also formally deprecated.) To play video content in iOS 9 and later, instead use the AVPictureInPictureController or AVPlayerViewController class from the AVKit framework, or the WKWebView class from WebKit
。我想,这意味着将来不会有MPMoviePlayerNotifications
通知。以上是关于如何替换 MPMoviePlayer 通知?的主要内容,如果未能解决你的问题,请参考以下文章