AVFoundation 混合音频

1,756 阅读2分钟

在组合媒体的过程中,需要用到 AVComposition 类,其中对于音频轨道可以使用与视频轨道一样的不重叠组合方式,同时音频轨道也可以重叠在一个时间段内,实现一种混音的效果,同时还可以设置不同轨道的音量变化。

1. 混音轨道

组合媒体时,分别生成了组合视频和音频的 AVMutableCompositionTrack 类,实现混音效果只需要在插入音频轨道时将时间轴直接覆盖即可。

                        AVMutableCompositionTrack *audioCompositionTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
                        AVAssetTrack *audioTracck = [[targetAsset tracksWithMediaType:AVMediaTypeAudio] firstObject];
                        [audioCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, targetAsset.duration) ofTrack:audioTracck atTime:kCMTimeZero error:nil];

这样生成的 AVMutableComposition 的多个音频轨道会同时发声。

2. 调节音量

要调节某个音频轨道的音量,需要用到 AVMutableAudioMix 类,这个类接受 AVMutableAudioMixInputParameters 对象作为参数,AVMutableAudioMixInputParameters 则提供了两种方法来设置音量,对于具体音量的变化范围定义为 0.0 到 1.0 之间。

  • 一段时间内变化音量 (void)setVolumeRampFromStartVolume:(float)startVolume toEndVolume:(float)endVolume timeRange:(CMTimeRange)timeRange;
  • 在某一时间点变化音量 - (void)setVolume:(float)volume atTime:(CMTime)time;

其具体使用如下

                            NSArray<AVAssetTrack *> *audioAssetTracks = [[composition copy] tracksWithMediaType:AVMediaTypeAudio]; // 获取音轨组合
                            AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
                            NSMutableArray<AVMutableAudioMixInputParameters *> *params = [NSMutableArray array];
                            __block CMTime cursor = kCMTimeZero;
                            [audioAssetTracks enumerateObjectsUsingBlock:^(AVAssetTrack * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { // 遍历音轨
                                AVMutableAudioMixInputParameters  *parameters = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:obj]; // 生成某一音轨的 AVMutableAudioMixInputParameters 对象
                                [parameters setVolumeRampFromStartVolume:begin toEndVolume:end timeRange:CMTimeRangeMake(cursor, obj.timeRange.duration)]; // 设置音量
                                begin = begin + end;
                                end = begin - end;
                                begin = begin - end;
                                cursor = CMTimeAdd(cursor, obj.timeRange.duration);
                                [params addObject:parameters]; // 加入到参数组合中
                            }];
                            audioMix.inputParameters = params;// 设置 audioMix 的参数组合

得到 audioMix 后,可以将其赋值给 AVPlayerItem 用于播放,也可以赋值给 AVAssetExportSession 用于导出完整的媒体资源,还可以赋值给 AVAssetReaderAudioMixOutput。

要注意的是,对于同一音轨,设置音频轨道的时间区间不能出现重叠,否则会抛出运行时异常

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'The timeRange of a volume ramp must not overlap the timeRange of an existing volume ramp.'