Cross-fade within AVMutableVideoComposition - iphone

I have successfully composed an AVMutableComposition with multiple video clips and can view it and export it, and I would like to be able to transition between them using a cross-fade, so I want to use AVMutableVideoComposition. I can't find any examples on how to even arrange and play a couple AVAsset videos in succession. Does anyone have an example of how to add tracks to an AVMutableVideoComposition with the equivalent of AVMutableComposition's insertTimeRange, or how to set up a cross-fade?
[self.composition insertTimeRange:CMTimeRangeMake(kCMTimeZero,asset.avAsset.duration)
ofAsset:asset.avAsset
atTime:self.composition.frameDuration
error:nil]

I found an example called AVEditDemo from Apple's WWDC 2010 Sample Code.
https://developer.apple.com/library/ios/samplecode/AVCustomEdit/Introduction/Intro.html
There is a lot of detail in the sample, but I'll summarize: You need to use both AVMutableComposition and AVMutableVideoComposition. Add tracks individually to AVMutableComposition instead of with the simpler insertTimeRange, as it allows you to set overlapping times on the tracks. The tracks also need to be added to the AVMutableVideoComposition as AVMutableVideoCompositionLayerInstructions with an opacity ramp. Finally, to play back in an AVPlayer, you need to create an AVPlayerItem using both the AVMutableComposition and AVMutableVideoComposition.
It seems like going each level deeper in the api – in this case from MPMoviePlayer with an asset to AVPlayer with an AVComposition and finally to an AVVideoComposition – increases necessary coding exponentially.

Related

add more than 1 video player in iphone

Is there any way to add more than 1 video player in a single View? I am getting the .m4v list from server adn have to display that much videos in single page.
It doesnot matter if I play 1 video at a time but which ever video I want must play there itself.
So the videos are placed one below other.
I have tried using mpmoviecontroller but it's drawback is it can have only one instance in the whole applicaiton So if i try to alloc 2 players in a view then the first one does not work and only one player works..
Is there any legal alternate way for the same?
MPMoviePlayerController will allow multiple instances, but only one of them can be playing their movie at any given time.
Please check the link : http://developer.apple.com/library/ios/#documentation/mediaplayer/reference/MPMoviePlayerController_Class/MPMoviePlayerController/MPMoviePlayerController.html
I used AVPlayer class (which also MPMoviePlayerController uses under the hood) to successfully play multiple videos from multiple source.
The process of using it is more involved than MPMoviePlayerController, but I would say it is definitely worth to look at if you want some customized behavior in your player. The official programming guide is a good start.

Playing a movie file and then various audio files while movie is playing back. (iOS)

Is it possible to play some audio files while my movie is playing. I don't want it to interrupt or pause he movie. Just the audio to play at same time at various predetermined points.
Many Thanks,
-Code
You shouldn't have any problems with it.
If you want to synchronize or delay these players you should use
the code they provide in AvAudioPlayer Class Reference using
playAtTime: method.
Sadly MPMediaPlayback protocol doesn't provide the same method
so exact synchronizing with video is a bit harder task.
EDIT: RoLYroLLs mentions using MPMoviePlayerLoadStateDidChangeNotification to achieve this here and this approach seems promising.
Use two or three Audioplayer at same time . Play that audio when u need it

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

Multy Video Player using monotouch

Here is the question:
Can a View in iPad to handle two different MpMoviePlayerView in the same Time
I have read that the moviePlayer is a singleton and I have to fix the observer with hard Code.
I am triying to do multiple Movie player in the same View??
Is it possible???
Any one have an idea???
MPMoviePlayerControllers won't let you play back two videos simultaneously. Sorry! Whilst you can create two MPMoviePlayer objects if you try and play two streams back at the same time it will just break. This is mentioned in the documentation for that class, buried away somewhere.

Fading in and out individual AVMutableCompositionTracks

I have multiple AVAssets, and I create individual AVMutableCompositionTracks for each. I then create an AVMutableComposition and add each AVMutableCompositionTrack to it and then create an AVAssetExportSession, init with the AVMutableComposition and run the exporter. This allows me to create a single audio file made up of many overlapping audio sources.
I can trim and delay each source audio file by setting the parameters when I insertTimeRange on each AVMutableCompositionTrack. What I can't figure out is how to fade in and out of each individual track. I can do a master fade on the export session by using setVolumeRampFromStartVolume via AVMutableAudioMixInputParameters, and I know how to do fades on AVPlayers using the same method, but I don't think AVMutableAudioMixInputParameters can be used on an AVMutableCompositionTrack, right?
So how can I add a fade to a AVMutableCompositionTrack?
Thanks!
AVMutableAudioMixInputParameters actually can be used with AVMutableCompositionTracks. I use them. It just isn't stored within the composition. Instead, you'll need to set the audioMix property of any AVPlayer or AVAssetExportSession you use.