在iOS编程中将混合(合并)声音拆分为单独的单元

时间:2013-11-13 13:02:02

标签: ios objective-c core-audio avmutablecomposition

我正在录制两个声音,然后使用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

1 个答案:

答案 0 :(得分:0)

通常,混合音频不能解除混合,分离或分离,除非完全不重叠且已知的频段,时隙或频道。