【问题标题】:Possible to insert item at top of queue using AVQueuePlayer?可以使用 AVQueuePlayer 在队列顶部插入项目吗?
【发布时间】:2014-07-01 04:03:55
【问题描述】:

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

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

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

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

【问题讨论】:

  • 怎么样:AVPlayerItem *currentItem = [yourAVPlayer currentItem]; [yourAVPlayer insertItem:currentItem afterItem:currentItem];?请注意,您可能必须执行 `[yourAVPlayer insertItem:[currentItem copy] afterItem:currentItem];`。
  • 我不确定它是如何工作的,因为我的新项目还没有被插入到队列中。但你实际上让我思考,我能够弄清楚。请参阅下面的答案。

标签: ios avplayer avqueueplayer


【解决方案1】:

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];

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

【讨论】:

    【解决方案2】:

    我知道这是一个老问题,但我今天又遇到了同样的问题。 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多