当项目未完成播放时,将发布AVPlayerItemDidPlayToEndTimeNotification

Tan*_*nya 6 objective-c ios avplayer

我正在使用AVPlayer播放来自网络的曲目.在我的播放列表中总有几首曲目.因此,为了定义当前轨道到达结束时将采取的操作,我使用KVO机制并注册发布的AVPlayerItemDidPlayToEndTimeNotification的观察者.

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

发布通知时,将调用itemReachedEnd方法:

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

事情是,有时这个方法在项目尚未完成播放时被调用,并且我已经在当前音轨播放结束之前切换了当前音轨.我不明白为什么会这样.

请告诉我,我做错了什么?也许我需要在跟踪切换之前考虑其他一些AVPlayerItem属性?

更新:我已经探索过当前轨道的当前位置不等于当前轨道持续时间.那么为什么玩家认为当前的项目已经完成了?

Cor*_*ius 1

不是你问题的真正答案,但你研究过吗AVQueuePlayer?这应该完全符合您的要求,而无需手动切换曲目。

编辑:您确定通知是针对当前项目的吗?或者您是否有一个观察者来观察之前播放过的项目,该项目因某种原因而被触发?