Associate AVPlayerItem with new AVPlayer in Swift?

How can I associate an instance AVPlayerItemwith a new one AVPlayerin Swift? When used in an initializer. Every time I try to reassign AVPlayerItemto another AVPlayereven after I set all the links AVPlayerto nil(so that it collects garbage), he complains that AVPlayerItemhe cannot be assigned to more than one object AVPlayer. I understand several ways around this, but I want to know why this method does not work.

let player: AVPlayer = AVPlayer(playerItem: somePlayerItem)

I don’t know what happens during this announcement, but something is installed somewhere to let somePlayerItem know that it has a related player. Does it have some set of observers somewhere or a set of properties that would notice this?

Does anyone know a way to reassign this AVPlayerItemto another AVPlayerafter the original links AVPlayerhave been destroyed? I know that I can just create a new one AVPlayerItemusing the URL, but I want to know if I can save the same object.

+4
source share
1 answer

You cannot reuse an element, but you can reuse it.

Create your resource once:

let asset=AVAsset(URL: your_url)

Then reuse it when necessary, creating a new element:

let item=AVPlayerItem(asset: asset)
let player=AVPlayer(playerItem: item)
+1
source

Source: https://habr.com/ru/post/1614540/


All Articles