我AVQueuePlayer
用来循环遍历数组AVPlayerItems
。我循环它的方式,我听它,AVPlayerItemDidPlayToEndTimeNotification
每次调用它时,我都会将当前对象添加到队列的末尾。继承人的代码:
viewWillAppear
{
...
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[_queuePlayer currentItem]];
[_queuePlayer play];
}
- (void)playerItemDidReachEnd:(NSNotification *)notification {
AVPlayerItem *p = [notification object];
AVPlayerItem *fakeNew = [[AVPlayerItem alloc] initWithAsset:p.asset];
if (_queuePlayer.items.count == 1)
{
[p seekToTime:kCMTimeZero];
[_queuePlayer play];
}
else
{
[_queuePlayer insertItem:fakeNew afterItem:[_queuePlayer.items lastObject]];
}
NSLog(@"array of items to play:%lu", (unsigned long)_queuePlayer.items.count);
}
问题是,该方法仅针对播放的第一个视频调用,之后,该方法停止被调用,因此,例如,如果我在数组中有 2 部电影,它将再次播放它们+第一个,任何想法为什么会这样?
更多信息:还尝试每次都制作一个新播放器并将其设置为图层。未能多次发送通知
- (void)playerItemDidReachEnd:(NSNotification *)notification {
AVPlayerItem *p = [notification object];
[self.playList removeObjectAtIndex:0];
[self.playList addObject:p];
AVPlayer *newPlayer = [[AVPlayer alloc] initWithPlayerItem:[self.playList objectAtIndex:0]];
_player = newPlayer;
self.AVPlayerLayerView.layer.player = self.player;
[_player play];
}