How to detect when an AVPlayerItem is finished playing?

3254523 picture 3254523 · May 6, 2014 · Viewed 16.7k times · Source

I've been looking through the AVPlayerItem and AVPlayer docs and there doesn't seem to be any callbacks for when the item is finished playing. I was hoping that there would be some sort of delegate callback that we can utilize or that AVPlayerActionAtItemEnd would provide a custom action for us to write.

How can i figure out a way to detect when AVPlayer has finished playing an item?

Answer

random picture random · May 6, 2014

It uses NSNotification to alert when playback is finished.

Register for the notification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];

Method to call when done:

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