iOS音轨合成

需求是录音+bgm,用AVMutableComposition创建录音和bgm的轨道(AVMutableCompositionTrack),两个音轨分别生成混音素材(AVMutableAudioMixInputParameters),再把两个素材添加到AVMutableAudioMix里,用AVAssetExportSession导出,目前导出格式只找到M4A(AVFileTypeAppleM4A)

//录音url
NSString *url = [self getFullFilePathInDocuments:@"record" fileName:@"selfRecord.caf"];

NSString *outputPath = [self getFullFilePathInDocuments:@"record" fileName:@"recordWithBgm.m4a"];
//存放音频混合参数的数组
NSMutableArray *mixParams = [NSMutableArray array];
//用来添加track轨道的混合素材
AVMutableComposition *composition = [[AVMutableComposition alloc]init];
    
//录音的轨道
AVMutableCompositionTrack *recordMutableTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
AVURLAsset *recordAsset = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:url] options:nil];
AVAssetTrack *recordTrack = recordAsset.tracks.firstObject;
NSError *recordInsertError;
[recordMutableTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, recordAsset.duration) ofTrack:recordTrack atTime:kCMTimeZero error:&recordInsertError];
if (recordInsertError) {
    NSLog(@"recordInsertError  %@",recordInsertError.description);
}
//从录音轨道中生成一个混音素材,添加到数组中
AVMutableAudioMixInputParameters *recordMix = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:recordMutableTrack];
//录音音量
[recordMix setVolume:100 atTime:CMTimeMake(0, 1)];
[mixParams appendObject:recordMix];
    
//bgm的轨道
AVMutableCompositionTrack *bgmMutableTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
AVURLAsset *bgmAsset = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:self.bgmUrlStr] options:nil];
AVAssetTrack *bgmTrack = bgmAsset.tracks.firstObject;
NSError *bgmInsertError;
CMTimeRange insertRange = CMTimeRangeMake(kCMTimeZero, recordAsset.duration);
if (progressSlider.value > 0) {
    CMTime currentTime = CMTimeMake(bgmAsset.duration.value / 100 * progressSlider.value, bgmAsset.duration.timescale);
    insertRange = CMTimeRangeMake(currentTime, recordAsset.duration);
}
[bgmMutableTrack insertTimeRange:insertRange ofTrack:bgmTrack atTime:kCMTimeZero error:&bgmInsertError];
if (bgmInsertError) {
    NSLog(@"bgmInsertError  %@",bgmInsertError.description);
}
AVMutableAudioMixInputParameters *bgmMix = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:bgmMutableTrack];
//bgm音量
[bgmMix setVolume:currentVolume / 5 atTime:CMTimeMake(0, 1)];
//插入bgm
[mixParams appendObject:bgmMix];
    
//创建一个可变的音频混音
AVMutableAudioMix *audioMix = [[AVMutableAudioMix alloc]init];
//将两个混音素材添加到混音对象中
audioMix.inputParameters = mixParams;
    
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:composition presetName:AVAssetExportPresetAppleM4A];
exportSession.outputFileType = AVFileTypeAppleM4A;
exportSession.audioMix = audioMix;
exportSession.outputURL = [NSURL fileURLWithPath:outputPath];
[exportSession exportAsynchronouslyWithCompletionHandler:^{
    dispatch_async(dispatch_get_main_queue(), ^{
        [hud hideAnimated:YES];
        [self.navigationController popViewControllerAnimated:YES];
    });
    if (exportSession.status == AVAssetExportSessionStatusCompleted) {
        NSLog(@"合成完毕  %f MB", [[NSData dataWithContentsOfURL:[NSURL fileURLWithPath:outputPath]] length] / 1024.00 / 1024.00);
    }
}];

你可能感兴趣的:(iOS音轨合成)