如何断开AVPlayer与AVPlayerItem的连接? [英] How to disconnect AVPlayer from AVPlayerItem?

查看:78
本文介绍了如何断开AVPlayer与AVPlayerItem的连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重用AVPlayerItem,但继续收到此错误:

I want to reuse an AVPlayerItem but keep getting this error:

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

在尝试重用它之前,我像这样破坏先前的AVPlayer:

Before trying to reuse it, I destroy the previous AVPlayer like this:

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

为什么AVPlayerItem仍然关联,我如何断开连接?

why is the AVPlayerItem still associated and how can I disconnect it?

这里是问题的完整摘要(仅50行): https://gist.github.com/sbiermanlytle/14a6faab515f7691b810789086ae9e50

Here is a Gist with a full reproduction of the problem (only 50 lines btw): https://gist.github.com/sbiermanlytle/14a6faab515f7691b810789086ae9e50

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

You can run it by creating a new Single View app, and supplanting the ViewController implementation with that code.

推荐答案

您不能断开AVPlayerItems的连接.我猜想指向播放器的私有属性并不弱,因此通过将当前播放器项设置为NULL来取消引用该项不会自动将该项的播放器属性设置为NULL.

You can't disconnect AVPlayerItems. I guess the private property that points to the player is not weak, so dereferencing the item by setting current player item to NULL does not automatically set the item's player property to NULL..

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

Simply create a new one. Either with the URL, in which case the cache system will return an AVAsset instantly ( Just another guess... ), or, better, with the asset of the PlayerItem you want to 'disconnect'.

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

这样做不会造成性能损失.该项目只是资产的句柄",其中包含真实数据.因此,不要害怕乱扔垃圾并即时创建新项目.

There is no performance loss doing this. The item is just a 'handle' to the asset, which contains the data for real. So don't be afraid to trash and create new items on the fly.

这篇关于如何断开AVPlayer与AVPlayerItem的连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆