Question

just want to understand , what exactly I can do with this AVMutableAudioMix class, once I will insert PlayerItems (Assets) in to the AudioMix, can I play them one at the time or some of them at the same time with AVPlayer and change the parameters dynamically?

Was it helpful?

Solution

I think you might be visualizing it incorrectly. An AVMutableAudioMix instance is actually a property on the AVPlayerItem class. First grab the track of the asset using tracksWithMediaType: and create an AVMutableAudioMixInputParameters instance using audioMixInputParametersWithTrack:. Set any audio properties on that inputparameters instance, (for instance setVolume:atTime).

Then you need to add the input parameters onto an AVMutableAudioMix instance. Then you need to add this to an player item. I know this sounds confusing, but this is exactly how AVFoundation works with just about everything. There's terms flying around all over the place, but pretty much everything has a hierarchy.

So the general hierarchy is this: player->playerItem->audioMix->inputParameters. The code to ramp down the volume from the 5 to 7 second mark should look something like this:

AVAssetTrack *audioTrack = [[self.player.currentItem.asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
AVMutableAudioMixInputParameters *params = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:audioTrack];
[params setVolumeRampFromStartVolume:1.0 toEndVolume:0.5 timeRange:CMTimeRangeMake(CMTimeMake(5,1), CMTimeMake(2,1))];

AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
audioMix.inputParameters = [NSArray arrayWithObject:params];

self.player.currentItem.audioMix = audioMix;

As far as doing this dynamically, you can, but only with local files (as opposed to streaming from the internet). I would probably try to keep this audioMix as an ivar and try resetting the params every time you want something to happen. If that doesn't work, you may have to create an instance of AVMutableAudioMix every time, not sure.

Also see this post and this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top