Reputation: 14237
Thanks to a stackoverflow user I am able to fade one AVPlayerTrack in or out. But I can't find a way to do both on a single track.
This is what I am trying to use:
-(void)fadeOutVolume
{
AVPlayerItem *myAVPlayerItem = self.songPlayer.currentItem;
AVAsset *myAVAsset = myAVPlayerItem.asset;
NSArray *audioTracks = [myAVAsset tracksWithMediaType:AVMediaTypeAudio];
NSMutableArray *allAudioParams = [NSMutableArray array];
for (AVAssetTrack *track in audioTracks) {
AVMutableAudioMixInputParameters *audioInputParams =
[AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];
CMTime fadeDuration = CMTimeMakeWithSeconds(5, 600);
CMTime fadeOutStartTime =
CMTimeMakeWithSeconds(CMTimeGetSeconds(myAVPlayerItem.duration)-5, 600);
CMTime fadeInStartTime = CMTimeMakeWithSeconds(0, 600);
[audioInputParams setVolumeRampFromStartVolume:1.0
toEndVolume:0
timeRange:CMTimeRangeMake(fadeOutStartTime, fadeDuration)];
AVMutableAudioMixInputParameters *audioInputParams2 =
[AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];
[audioInputParams2 setVolumeRampFromStartVolume:0.0
toEndVolume:1.0
timeRange:CMTimeRangeMake(fadeInStartTime, fadeDuration)];
[allAudioParams addObject:audioInputParams];
[allAudioParams addObject:audioInputParams2];
}
AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
[audioMix setInputParameters:allAudioParams];
[myAVPlayerItem setAudioMix:audioMix];
}
This works if I comment [allAudioParams addObject:audioInputParams];
or the other line. But I can't have the two input params at the same time.
Upvotes: 4
Views: 1356
Reputation: 14237
I have found the problem. I don't need two audioInputParams. In fact, it seems like they override somehow.
If I delete the new audioInputParam and do the two set ramps it all works well. Actually this is kinda missleading. SetRamp it seems like is going to override and the "addObject" is the one that overrides. I ended up with this code in the for part:
AVMutableAudioMixInputParameters *audioInputParams =
[AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];
CMTime fadeDuration = CMTimeMakeWithSeconds(5, 600);
CMTime fadeOutStartTime = CMTimeMakeWithSeconds(CMTimeGetSeconds(myAVPlayerItem.duration)-5, 600);
CMTime fadeInStartTime = CMTimeMakeWithSeconds(0, 600);
[audioInputParams setVolumeRampFromStartVolume:1.0 toEndVolume:0 timeRange:CMTimeRangeMake(fadeOutStartTime, fadeDuration)];
[audioInputParams setVolumeRampFromStartVolume:0.0 toEndVolume:1.0 timeRange:CMTimeRangeMake(fadeInStartTime, fadeDuration)];
[allAudioParams addObject:audioInputParams];
Thanks @Mundi for the attempt to help me out.
Upvotes: 3
Reputation: 80265
You can only set the start volume ramp once. This is sort of logical, right?
If you want to change the volume again later, use
setVolume:atTime:
This method adds a volume ramp starting at time. This volume setting remains in effect until the end of the track unless you set a different volume level to start at a later time.
EDIT:
Maybe this answer is not entirely correct. There is nothing in the docs that would indicate this restriction.
But I noticed that you iterate through the tracks and set the fadeOutStratTime
based on myAVPlayerItem
- perhaps there is a logic mistake leading to the error.
Upvotes: 1