我正在使用AVPlayer播放Web上的轨道。在我的播放列表中,总是有几首轨道。
因此,为了定义当当前音轨到达其结尾时将要采取的 Action ,我使用KVO机制并为发布的AVPlayerItemDidPlayToEndTimeNotification注册一个观察者。
if (_player.currentItem.status == AVPlayerItemStatusReadyToPlay)
{
[self play];
[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemReachedEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:_player.currentItem];
}
发布通知时,将调用itemReachedEnd方法:
- (void) itemReachedEnd:(NSNotification *) notification
{
dispatch_async(dispatch_get_main_queue(), ^{
//switching to the next track
}
问题是,有时在该项目尚未完成播放并且在播放到结尾之前切换了当前轨道时会调用此方法。我不明白为什么会这样。
请告诉我,我在做什么错?也许在切换轨道之前需要考虑其他一些AVPlayerItem属性?
Upd:我已经探索了当前轨道的当前位置不等于当前轨道的持续时间。那么玩家为什么认为当前项目已经完成播放呢?
最佳答案
这不是您问题的真正答案,但是您调查过 AVQueuePlayer
吗?无需手动切换音轨,即可完全满足您的要求。
编辑:您确定通知是针对当前商品的吗?还是您可能因为某个原因而触发了先前播放的项目的旁观者?