How to disconnect AVPlayer from AVPlayerItem?

白昼怎懂夜的黑 提交于 2019-12-24 06:24:07

问题


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

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

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

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

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

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

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


回答1:


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..

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.



来源:https://stackoverflow.com/questions/37958398/how-to-disconnect-avplayer-from-avplayeritem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!