我有一个实例,AVQueuePlayer
我已经使用一组视频对其进行了初始化AVPlayerItem
(假设我有 10 个项目),在按钮事件上,我需要播放集合中第 n 个索引的项目。
我用过- (void)replaceCurrentItemWithPlayerItem:(AVPlayerItem *)item
,但它用传递的AVPlayerItem
.
我需要玩第 nAVPlayerItem
个AVQueuePlayer
。任何帮助将不胜感激。
我有一个实例,AVQueuePlayer
我已经使用一组视频对其进行了初始化AVPlayerItem
(假设我有 10 个项目),在按钮事件上,我需要播放集合中第 n 个索引的项目。
我用过- (void)replaceCurrentItemWithPlayerItem:(AVPlayerItem *)item
,但它用传递的AVPlayerItem
.
我需要玩第 nAVPlayerItem
个AVQueuePlayer
。任何帮助将不胜感激。
Two ideas, not tested:
A. Advancing
NSUInteger indexToPlay = …
AVPlayerItem *currentItem = [queuePlayer currentItem];
NSUInteger currentIndex = [[queuePlayer items] indexOfObject:currentItem];
for (;currentIndex<indexToPlay; currentIndex++)
{
[queuePlayer advanceToNextItem];
}
B. Calculating the time
Get the duration using -duration
(AVPlayerItem
) of all videos from the beginning of the queue up to (but not including) the nth video and go to the resulting time with
-seekToTime:toleranceBefore:toleranceAfter:
.
I think that you should have no problem with tolerance and decoding delay, because the seemed time is the beginning of a video.
- (void)playAudioAtIndex:(NSInteger)index
{
[player removeAllItems];
for (int i = index; i <playerItems.count; i ++) {
AVPlayerItem* obj = [playerItems objectAtIndex:i];
if ([player canInsertItem:obj afterItem:nil]) {
[obj seekToTime:kCMTimeZero];
[player insertItem:obj afterItem:nil];
}
}
}
playerItems 是您存储 AVPlayerItems 的 NSMutableArray(NSArray)。