Search code examples
objective-cmedia-playeravplayerfadeoutavplayeritem

Volume fadeOut using AVMutableAudioMixInputParameters


I want to fadeout the sound track when the user clicks on the stop button for example, I use this code for fading out the sound

-(void)soundFadeOut{

AVPlayerItem *myAVPlayerItem = mainPlayer.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];
[myAVPlayerItem setAudioMix:audioMix];}

but when I click on the button the sound volume goes to Zero at one time.


Solution

  • I know my fault now, it was in the first CMTime parameter in the CMTimeRange

            CMTimeRangeMake(CMTimeMake(0, 1), CMTimeMake( 5, 1);
    

    I found that the first CMTimeMake points to the second that will start fading out in my case the current second, and the other CMTimeMake is for the duration of fading out