Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't my AVPlayer volume fading out?

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.

like image 368
zakdances Avatar asked Oct 07 '22 02:10

zakdances


1 Answers

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.

like image 67
zakdances Avatar answered Oct 12 '22 13:10

zakdances