AVPlayerItemDidPlayToEndTimeNotification is published when the item has not finished playing

I use AVPlayer to play tracks from the Internet. There are always a few tracks in my playlist. So, to determine the action that will be performed when the current track reaches its end, I use the KVO mechanism and register an observer for the published AVPlayerItemDidPlayToEndTimeNotification.

if (_player.currentItem.status == AVPlayerItemStatusReadyToPlay) { [self play]; [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemReachedEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:_player.currentItem]; } 

When the notification is sent, the itemReachedEnd method is called:

 - (void) itemReachedEnd:(NSNotification *) notification { dispatch_async(dispatch_get_main_queue(), ^{ //switching to the next track } 

The fact is that sometimes this method is called when the element has not finished playing, and I have the current track, which was switched before it was played to the end. I do not understand why this is happening.

Please tell me what am I doing wrong? Maybe I need to take into account some other properties of AVPlayerItem before switching tracks?

Update: I researched that the current position of the current track is not equal to the current track duration. Then why does the player think that the current element has finished the game?

+6
source share
1 answer

Not a real answer to your question, but have you viewed AVQueuePlayer ? This should do exactly what you want, without having to manually switch the track.

Edit: Are you sure the notification is for the current item? Or do you possibly have an observer for a previously played item that is triggered for some reason?

+1
source

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


All Articles