在组合媒体的过程当中,须要用到 AVComposition 类,其中对于音频轨道能够使用与视频轨道同样的不重叠组合方式,同时音频轨道也能够重叠在一个时间段内,实现一种混音的效果,同时还能够设置不一样轨道的音量变化。数组
组合媒体时,分别生成了组合视频和音频的 AVMutableCompositionTrack 类,实现混音效果只须要在插入音频轨道时将时间轴直接覆盖便可。bash
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 的多个音频轨道会同时发声。app
要调节某个音频轨道的音量,须要用到 AVMutableAudioMix 类,这个类接受 AVMutableAudioMixInputParameters 对象做为参数,AVMutableAudioMixInputParameters 则提供了两种方法来设置音量,对于具体音量的变化范围定义为 0.0 到 1.0 之间。ui
其具体使用以下spa
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。code
要注意的是,对于同一音轨,设置音频轨道的时间区间不能出现重叠,不然会抛出运行时异常视频
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'The timeRange of a volume ramp must not overlap the timeRange of an existing volume ramp.'
复制代码