I'm trying to fade my AVPlayer's volume to 0 using AVMutableAudioMixInputParameters's setVolumeRampFromStartVolume method. Here is my code:
-(void)fadeOutVolume
{
// AVPlayerObject is a property which points to an AVPlayer
AVPlayerItem *myAVPlayerItem = AVPlayerObject.currentItem;
AVAsset *myAVAsset = myAVPlayerItem.asset;
NSArray *audioTracks = [myAVAsset tracksWithMediaType:AVMediaTypeAudio];
NSMutableArray *allAudioParams = [NSMutableArray array];
for (AVAssetTrack *track in audioTracks) {
AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];
[audioInputParams setVolumeRampFromStartVolume:1.0 toEndVolume:0 timeRange:CMTimeRangeMake(CMTimeMake(0, 1), CMTimeMake(5, 1))];
[allAudioParams addObject:audioInputParams];
}
AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
[audioMix setInputParameters:allAudioParams];
}
Can anyone see what's wrong with this code? It's doesn't fade out the volume correctly.
I was missing this key line:
[myAVPlayerItem setAudioMix:audioMix];
This was a relatively easy fix and I'm disappointed the usually super-quick and observant StackOverflow community didn't spot the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With