我正在录制两种声音,然后使用 AVMutableComposition 和 AVAudioMix 将它们混合。我可以使用给定的代码来实现这一点
-(void)saveRecording
{
AVMutableComposition *composition = [AVMutableComposition composition];
audioMixParams = [[NSMutableArray alloc] initWithObjects:nil];
//IMPLEMENT FOLLOWING CODE WHEN WANT TO MERGE ANOTHER AUDIO FILE
//Add Audio Tracks to Composition
NSString *docsDir;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString *URLPath1 = [docsDir
stringByAppendingPathComponent:@"sound1.caf"];
NSString *URLPath2 = [docsDir
stringByAppendingPathComponent:@"sound2.caf"];
NSURL *assetURL1 = [NSURL fileURLWithPath:URLPath1];
[self setUpAndAddAudioAtPath:assetURL1 toComposition:composition];
NSURL *assetURL2 = [NSURL fileURLWithPath:URLPath2];
[self setUpAndAddAudioAtPath:assetURL2 toComposition:composition];
AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
audioMix.inputParameters = [NSArray arrayWithArray:audioMixParams];
//If you need to query what formats you can export to, here's a way to find out
NSLog (@"compatible presets for songAsset: %@",
[AVAssetExportSession exportPresetsCompatibleWithAsset:composition]);
AVAssetExportSession *exporter = [[AVAssetExportSession alloc]
initWithAsset: composition
presetName:AVAssetExportPresetAppleM4A];
// NSFileManager *fileManager=[NSFileManager defaultManager];
/*Get the Path to Application Documents Directory*/
NSArray *docDir=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
/*append the neccessary file extension */
NSString *exportFile=[NSString stringWithFormat:@"/%@/mixed.mp4",docDir];
NSLog(@"document directory path %@",exportFile);
exporter.audioMix = audioMix;
exporter.outputFileType = @"com.apple.m4a-audio";
NSURL *exportURL = [NSURL fileURLWithPath:exportFile];
exporter.outputURL = exportURL;
// do the export
[exporter exportAsynchronouslyWithCompletionHandler:^{
int exportStatus = exporter.status;
NSError *exportError = exporter.error;
switch (exportStatus) {
case AVAssetExportSessionStatusFailed:
NSLog (@"%@",exportError.description);
break;
case AVAssetExportSessionStatusCompleted:{
mixingStatusl=YES;
NSLog (@"AVAssetExportSessionStatusCompleted");
}
break;
case AVAssetExportSessionStatusUnknown: NSLog (@"AVAssetExportSessionStatusUnknown"); break;
case AVAssetExportSessionStatusExporting: NSLog (@"AVAssetExportSessionStatusExporting"); break;
case AVAssetExportSessionStatusCancelled: NSLog (@"AVAssetExportSessionStatusCancelled"); break;
case AVAssetExportSessionStatusWaiting: NSLog (@"AVAssetExportSessionStatusWaiting"); break;
default: NSLog (@"didn't get export status"); break;
}
if(mixingStatusl)
{
NSArray *docDir=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
/*append the neccessary file extension */
NSString *exportFile=[NSString stringWithFormat:@"/%@/mixed.mp4",docDir];
NSURL *url = [NSURL fileURLWithPath:exportFile];
NSError *error;
audioPlayer1 = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
if (error)
{
NSLog(@"Error in audioPlayer: %@",
[error localizedDescription]);
} else {
audioPlayer1.delegate = self;
[audioPlayer1 prepareToPlay];
}
[audioPlayer1 play];
}
}];
}
现在我需要将最终的声音(mixed.mp4)拆分成之前混合的单独的声音。请建议如何继续拆分mixed.mp4