Now that MPMoviePlayerPlaybackDidFinishReasonUserInfoKey is out of date, what is the best way to find why playback ended?

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

The following are the keys to complete:

  • MPMovieFinishReasonPlaybackEnded
  • MPMovieFinishReasonPlaybackError
  • MPMovieFinishReasonUserExited
+4
source share
2 answers

AVKit MPMoviePlayerPlaybackDidFinishReasonUserInfoKey MPMoviePlayerPlaybackDidFinishNotification AVKit. AVKit, , .

  • MPMovieFinishReasonPlaybackEnded → > AVPlayerItemDidPlayToEndTimeNotification
  • MPMovieFinishReasonPlaybackError → > AVPlayerItemFailedToPlayToEndTimeNotification
  • MPMovieFinishReasonUserExited. . , . - .

, , KVO:

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

:

- (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
        }
    }
}
+4

viewDidLoad:

    AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:@"yoururl"]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];

    AVPlayer* player = [[[AVPlayer alloc] initWithPlayerItem:playerItem] autorelease];

    [player play]

-(void)itemDidFinishPlaying:(NSNotification *) notification {
    // Will be called when AVPlayer finishes playing playerItem
}
+1

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


All Articles