如何替换MPMoviePlayer通知? [英] How do I replace MPMoviePlayer notifications?

查看:282
本文介绍了如何替换MPMoviePlayer通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In iOS 9 MPMoviePlayer and all his components are deprecated. We used MPMoviePlayerController notifications, like MPMoviePlayerLoadStateDidChangeNotification, MPMovieDurationAvailableNotification, MPMoviePlayerPlaybackStateDidChangeNotification, MPMoviePlayerReadyForDisplayDidChangeNotification, to track video service quality. But now with AVPlayerViewController I can't find properly replacement for these notifications.

如何立即替换这些通知?

How do I replace these notifications now?

推荐答案

AVPlayerViewController MPMoviePlayerViewController 的用法有很大不同。您可以使用Key Value Observing来确定与 AVPlayerViewController 相关联的 AVPlayer 对象的当前特征,而不是使用通知。根据文档:

AVPlayerViewController is a lot different in its usage from the MPMoviePlayerViewController. Instead of using notifications you use Key Value Observing to determine the current characteristics of the AVPlayer object associated with the AVPlayerViewController. According to the docs:


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

You can observe the status of a player using key-value observing. So that you can add and remove observers safely, AVPlayer serializes notifications of changes that occur dynamically during playback on a dispatch queue. By default, this queue is the main queue (see dispatch_get_main_queue). To ensure safe access to a player’s nonatomic properties while dynamic changes in playback state may be reported, you must serialize access with the receiver’s notification queue. In the common case, such serialization is naturally achieved by invoking AVPlayer’s various methods on the main thread or queue.

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

For instance, if you want to know when your player has been paused add an observer on the rate property of the AVPlayer object:

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

然后在observe方法中检查 new value等于零:

Then in the observe method check if the new value is equal to zero:

- (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上的很多属性是可观察的。浏览课程参考

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

Also apart from this there are several Notifications available for the AVPlayerItem object which are limited but still helpful.


通知

Notifications

AVPlayerItemDidPlayToEndTimeNotification

AVPlayerItemDidPlayToEndTimeNotification

AVPlayerItemFailedToPlayToEndTimeNotification

AVPlayerItemFailedToPlayToEndTimeNotification

AVPlayerItemTimeJumpedNotification

AVPlayerItemTimeJumpedNotification

AVPlayerItemPlaybackStalledNotification

AVPlayerItemPlaybackStalledNotification

AVPlayerItemNewAccessLogEntryNotification

AVPlayerItemNewAccessLogEntryNotification

AVPlayerItemNewErrorLogEntryNotification

AVPlayerItemNewErrorLogEntryNotification

我找到 AVPlayerItemDidPlayToEndTimeNotification 一旦播放结束就将项目设置为开头特别有用。

I find AVPlayerItemDidPlayToEndTimeNotification particularly useful to seek the Item to the start once playback has finished.

一起使用这两个选项,你应该可以替换大部分并非所有 MPMoviePlayerController的通知

Using these two options together you should be able to replace most if not all notifications for the MPMoviePlayerController

这篇关于如何替换MPMoviePlayer通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆