zakdances
zakdances

Reputation: 23685

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.

Upvotes: 6

Views: 3170

Answers (2)

Vinodh
Vinodh

Reputation: 5268

    [audioInputParams setVolumeRampFromStartVolume:1.0 toEndVolume:0 timeRange:CMTimeRangeMake(CMTimeMake(0, 1), CMTimeMake(5, 1))];

in this you set start to 1.0 and end to 0 so only audio was getting fade out

Upvotes: -2

zakdances
zakdances

Reputation: 23685

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.

Upvotes: 7

Related Questions