Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVPlayerItemDidPlayToEndTimeNotification is posted when the item did not finish playing

I am using the AVPlayer for playing tracks from web. In my playlist there are always several tracks. So, to define the action that will be taken when the current track reaches its end, I use the KVO mechanism and register an observer for the AVPlayerItemDidPlayToEndTimeNotification posted.

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

When notification is posted the itemReachedEnd method is called:

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

The thing is, sometimes this method is called when the item has not yet finished playing and I have the current track switched before it has played to the end. I do not understand why is this happening.

Please, tell me, what am I doing wrong? Maybe I need to take into consideration some other AVPlayerItem properties before track switching?

Upd: I have explored that the current position of the current track is not equal to the current track duration. Then why does the player thinks that the current item has finished playing?

like image 913
Tanya Avatar asked Jun 19 '14 16:06

Tanya


Video Answer


1 Answers

Not a real answer to your question, but have you looked into AVQueuePlayer? That should do exactly what you want without the need to switch the track manually.

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

like image 102
Cornelius Avatar answered Oct 19 '22 09:10

Cornelius