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?
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With