【问题标题】:How to disconnect AVPlayer from AVPlayerItem?如何断开 AVPlayer 与 AVPlayerItem 的连接?
【发布时间】:2016-06-22 04:09:54
【问题描述】:

我想重用 AVPlayerItem,但不断收到此错误:

An AVPlayerItem cannot be associated with more than one instance of AVPlayer

在尝试重用它之前,我像这样销毁以前的 AVPlayer:

[self.player pause];
[self.player replaceCurrentItemWithPlayerItem:nil];
self.player = nil;

为什么 AVPlayerItem 仍然关联,我该如何断开它?

这是一个完整再现问题的要点(顺便说一句,只有 50 行):https://gist.github.com/sbiermanlytle/14a6faab515f7691b810789086ae9e50

您可以通过创建一个新的单视图应用程序并用该代码替换 ViewController 实现来运行它。

【问题讨论】:

    标签: ios avfoundation avplayer


    【解决方案1】:

    您无法断开 AVPlayerItems。我猜指向播放器的私有属性并不弱,所以通过将当前播放器项目设置为 NULL 来取消引用该项目不会自动将项目的播放器属性设置为 NULL..

    只需创建一个新的。要么使用 URL,在这种情况下,缓存系统将立即返回 AVAsset(只是另一个猜测......),或者,更好的是,使用您想要“断开”的 PlayerItem 的资产。

    AVPlayerItem* newPlayerItem = [AVPlayerItem playerItemWithAsset:playerItem.asset];
    

    这样做不会造成性能损失。该项目只是资产的“句柄”,其中包含真实的数据。因此,不要害怕丢弃并即时创建新项目。

    【讨论】:

    • 你能给个swift代码吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    相关资源
    最近更新 更多