Reloading AVMutableComposition

2.6k views Asked by At

I have a AVMutableComposition that I've associated with an AVPlayer. It seems if I add tracks to this composition the AVPlayer doesn't play the added tracks unless I set the composition on the player again. Right now I'm doing this by just replacing the current item with the same composition that is already set. After that it picks up the new tracks.

I haven't been able to figure out if this is the correct way of doing it, it somehow seems a bit strange having to do it like that.

Any ideas?

Thanks

2

There are 2 answers

1
Or Ron On BEST ANSWER

Basically although it's mutable composition, apple recommend to create an immutable composition and create AVPlayerItem for playback purposes. It's a bit annoying but you can go around it. When you want to refresh the video just sample the time, create another AVPlayerItem and load it to the right time.

Here's snippet from apple's AVMutableComposition documentation

AVMutableComposition is a mutable subclass of AVComposition you use when you want to create a new composition from existing assets. You can add and remove tracks, and you can add, remove, and scale time ranges.

You can make an immutable snapshot of a mutable composition for playback or inspection as follows:

AVMutableComposition *myMutableComposition =
    <#a mutable composition you want to inspect or play in its current state#>;

AVComposition *immutableSnapshotOfMyComposition = [myMutableComposition copy];

// Create a player to inspect and play the composition.
AVPlayerItem *playerItemForSnapshottedComposition =
    [[AVPlayerItem alloc] initWithAsset:immutableSnapshotOfMyComposition];

Further information: Full Document

0
Tien Nguyen On

If you just remove a track from the mutable composition which has associated with the playerItem, you can call the seeking method on the AVPlayer object to the current time with a completion handler to force the player clearing the loaded buffer.

// refresh player
player.seek(to: playerItem.currentTime()) { _ in }