How to replace MPMoviePlayer notifications?

In iOS 9, MPMoviePlayer and all its components are deprecated. We used MPMoviePlayerController notifications, for example MPMoviePlayerLoadStateDidChangeNotification, MPMovieDurationAvailableNotification, MPMoviePlayerPlaybackStateDidChangeNotification, MPMoviePlayerReadyForDisplayDidChangeNotification , to track the quality of the video services. But now with AVPlayerViewController I can’t find a suitable replacement for these notifications.

How to replace these notifications now?

+5
source share
2 answers

AVPlayerViewController lot different than using 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:

You can monitor the player’s status using key monitoring. So that you can safely add and remove observers, AVPlayer serializes notifications of changes that occur dynamically during playback on the send queue. By default, this queue is the main queue (see dispatch_get_main_queue). To provide secure access to players non-atomic properties, while dynamic changes in the playback state may be reported, you must serialize access with notification to the recipients queue. In the general case, such serialization is naturally achieved by calling AVPlayers various methods in the main thread or queue.

For example, if you want to know when your player is paused, add an observer to the rate property of the AVPlayer object:

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

Then, in the observation method, verify that the new value is 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; } 

There are many properties on AVPlayer . Go to Class Link .

In addition, there are several notifications available for the AVPlayerItem object, which are limited but still useful.

Notifications

AVPlayerItemDidPlayToEndTimeNotification

AVPlayerItemFailedToPlayToEndTimeNotification

AVPlayerItemTimeJumpedNotification

AVPlayerItemPlaybackStalledNotification

AVPlayerItemNewAccessLogEntryNotification

AVPlayerItemNewErrorLogEntryNotification

I find AVPlayerItemDidPlayToEndTimeNotification particularly useful for finding an item at the beginning after playback is completed.

Using these two options together, you can replace most, if not all, notifications for MPMoviePlayerController

+5
source

I looked at the documentation for both MPMoviePlayerNotifications and AVPlayerItemNotifications , and I noticed two things.

So, I'm confused that you are saying that MPMoviePlayerNotifications are out of date because the docs say they are available. In addition, I do not think that AVPlayerItemNotifications has a replacement for MPMoviePlayerNotifications .

+1
source

Source: https://habr.com/ru/post/1232536/


All Articles