2

我有一个实例,AVQueuePlayer我已经使用一组视频对其进行了初始化AVPlayerItem(假设我有 10 个项目),在按钮事件上,我需要播放集合中第 n 个索引的项目。

我用过- (void)replaceCurrentItemWithPlayerItem:(AVPlayerItem *)item,但它用传递的AVPlayerItem.

我需要玩第 nAVPlayerItemAVQueuePlayer。任何帮助将不胜感激。

4

2 回答 2

5

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.

于 2015-04-22T08:47:39.883 回答
3
- (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)。

于 2015-04-22T07:34:10.837 回答