Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Now that MPMoviePlayerPlaybackDidFinishReasonUserInfoKey is deprecated, what is the best way to find why playback ended?

In iOS9, the MPMoviePlayer classes have all been deprecated in favor of AVPlayer. I have an existing app using MPMoviePlayerPlaybackDidFinishReasonUserInfoKey to determine how to log events on how the video player ended. How do I do the same with AVPlayer?

The following are the ending reason keys:

  • MPMovieFinishReasonPlaybackEnded
  • MPMovieFinishReasonPlaybackError
  • MPMovieFinishReasonUserExited
like image 730
Booyah Avatar asked Mar 15 '23 10:03

Booyah


1 Answers

There is no equivalent to MPMoviePlayerPlaybackDidFinishReasonUserInfoKey and MPMoviePlayerPlaybackDidFinishNotification in AVKit. To accomplish the same functionality in AVKit, you must listen to three notifications separately, rather than one notification with different possible reasons.

  • MPMovieFinishReasonPlaybackEnded >>> AVPlayerItemDidPlayToEndTimeNotification
  • MPMovieFinishReasonPlaybackError >>> AVPlayerItemFailedToPlayToEndTimeNotification
  • MPMovieFinishReasonUserExited. No conversion. There are multiple ways to detect that the user has killed the player. One is detecting a modal has closed.

If you want to know if the video is playing or not, you can do a KVO:

[self.player addObserver:self forKeyPath:@"rate" options:0 context:nil];

Then add this method:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{
    if ([keyPath isEqualToString:@"rate"]) {
        if ([self.player rate]) {
            [self changeToPause];  // This changes the button to Pause
        }
        else {
            [self changeToPlay];   // This changes the button to Play
        }
    }
}
like image 90
Booyah Avatar answered May 08 '23 00:05

Booyah