iOS AVFoundation音频 混合、拼接、播放音量设置
2019-06-01 本文已影响0人
如意神王
音频 混合-拼接代码段
-(void)addAudioPath1:(NSURL *)firstPath andAudioPath2:(NSURL *)secondPath {
// 第一个音频内容
AVAsset * firstAudioAsset = [AVAsset assetWithURL:firstPath];
// 第二个音频内容
AVAsset * secondAudioAsset = [AVAsset assetWithURL:secondPath];
// 创建可变的音频视频组合 音频组合 音视频组合 视频组合都使用这个
AVMutableComposition *mixComposition = [AVMutableComposition composition];
// 第一个音频轨道
AVMutableCompositionTrack *firstAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
// 第一个音频asset添加到轨道,第一个音频内容添加到firstAudioTrack轨道中
// 参数1:时间0-firstAudioAsset音频的持续时间
// 参数2:AVAssetTrack类型的内容
// 参数3:开始时间
[firstAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAudioAsset.duration) ofTrack:[firstAudioAsset tracksWithMediaType:AVMediaTypeAudio].firstObject atTime:kCMTimeInvalid error:nil];
// 第二个音频轨道
AVMutableCompositionTrack *secondAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
//第二个音频asset添加到轨道,开始时间不同就决定了音频混合或者拼接
// atTime:kCMTimeInvalid 混音 音频1和音频2 同时播放
// atTime:firstAudioAsset.duration 拼接 音频2的播放在音频1结束以后开始
[secondAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, secondAudioAsset.duration) ofTrack:[secondAudioAsset tracksWithMediaType:AVMediaTypeAudio].firstObject atTime:kCMTimeInvalid error:nil];
NSLog(@"%@", [secondAudioAsset tracksWithMediaType:AVMediaTypeAudio].firstObject);
//修改背景音乐的音量start
AVMutableAudioMix *videoAudioMixTools = [AVMutableAudioMix audioMix];
NSMutableArray * params = [[NSMutableArray alloc] initWithCapacity:0];
if (firstAudioAsset) {
//调节音量
//获取音频轨道
AVMutableAudioMixInputParameters *firstAudioParam = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:firstAudioTrack];
//设置音轨音量,可以设置渐变,设置为1.0就是全音量
[firstAudioParam setVolumeRampFromStartVolume:0.05 toEndVolume:1.0 timeRange:CMTimeRangeMake(kCMTimeZero, firstAudioAsset.duration)];
[firstAudioParam setTrackID:firstAudioTrack.trackID];
[params addObject:firstAudioParam];
}
if (secondAudioAsset) {
//调节音量
//获取音频轨道
AVMutableAudioMixInputParameters *secondAudioParam = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:secondAudioTrack];
//设置音轨音量,可以设置渐变,设置为1.0就是全音量
[secondAudioParam setVolumeRampFromStartVolume:0.1 toEndVolume:1.0 timeRange:CMTimeRangeMake(kCMTimeZero, secondAudioAsset.duration)];
[secondAudioParam setTrackID:secondAudioTrack.trackID];
[params addObject:secondAudioParam];
}
videoAudioMixTools.inputParameters = [NSArray arrayWithArray:params];
//end
//导出音频
AVAssetExportSession *session = [[AVAssetExportSession alloc]initWithAsset:mixComposition presetName:AVAssetExportPresetAppleM4A];
NSString * outputPath = [self getAudioPath];
self.saveAudioPath = outputPath;
// 千万注意 fileURLWithPath
session.outputURL = [NSURL fileURLWithPath:outputPath];
session.outputFileType = AVFileTypeAppleM4A;
session.shouldOptimizeForNetworkUse = YES;
session.audioMix = videoAudioMixTools;
//导出之前先删除之前旧文件
//导出是否成功回调
[session exportAsynchronouslyWithCompletionHandler:^{
NSLog(@"exportSession.status == %ld", (long)session.status);
if (session.status == AVAssetExportSessionStatusCompleted) {
NSLog(@"音频拼接成功");
[self playAudio];
} else {
NSLog(@"音频拼接失败");
}
}];
}
-(void)playAudio {
if (!self.musicPlayer) {
NSURL *fileUrl = [NSURL URLWithString:self.saveAudioPath];
self.musicPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:fileUrl error:nil];
}
if (![self.musicPlayer isPlaying]){
[self.musicPlayer setVolume:1.0];
[self.musicPlayer prepareToPlay];
[self.musicPlayer play];
}
}