Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reloading AVMutableComposition

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

like image 917
Kenny Lövrin Avatar asked Jan 24 '12 21:01

Kenny Lövrin


1 Answers

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

like image 135
Or Ron Avatar answered Sep 28 '22 23:09

Or Ron