2

假设队列已经初始化,我看到的将项目添加到队列中的唯一方法是:

- (void)insertItem:(AVPlayerItem *)item afterItem:(AVPlayerItem *)afterItem

文件说Pass nil to append the item to the queue.

那么是不是不能将一个项目添加到队列的顶部?我希望能够重播以前播放的内容,而无需再次删除和重新排队所有内容。

4

2 回答 2

5

Larme 上面的评论让我思考,我实际上能够通过执行以下操作来模仿我正在寻找的行为:

// pause the player since we're messing with the currently playing item
[_avQueuePlayer pause];

// get current first item
id firstItem = [_avQueuePlayer.items objectAtIndex:0];

// add new item in 2nd spot
[_avQueuePlayer insertItem:newItem afterItem:firstItem];

// remove our first item so the new item becomes first in line
[_avQueuePlayer removeItem:firstItem];

// now add the original first item back in after the newly insert item
[_avQueuePlayer insertItem:firstItem afterItem:newItem];

// continue playing again
[_avQueuePlayer play];

这很好用!我认为唯一的缺点是玩家必须再次缓冲我们删除并重新插入的下一个项目。但是队列中的剩余项目将保持缓冲,因此这比必须重置整个队列要好。

于 2014-07-01T17:49:06.787 回答
0

我知道这是一个老问题,但我今天又遇到了同样的问题。Oren 的解决方案对我没有奏效,所以我采用了更激进的方法。我的情况也不同,因为我在队列中只有两个项目(因此我使用removeAll):

if let currentItem = player.currentItem {
    player.removeAllItems()
    player.insert(item, after: nil)
    player.insert(AVPlayerItem(asset: currentItem.asset), after: item)
} else {
    player.insert(item, after: nil)
}

currentItem注意:在删除后再次插入是不够的,因为之后玩家items()仍然只是返回了 1 个项目(item),即使调用canInsert(currentItem, after: item)返回true

于 2021-04-13T12:42:17.023 回答