简体   繁体   English

如何通过在iphone中混合多个音频来录制音频?

[英]How to record an audio by mixing multiple audio in iphone?

I have to record an audio by mixing multiple audio file.我必须通过混合多个音频文件来录制音频。 For example, if three audio file are being played, so I have to mix the sound of all those playing audio and record it into a single audio file.例如,如果正在播放三个音频文件,那么我必须将所有播放音频的声音混合并将其录制为单个音频文件。 Please help me in this in this, if any working code is there to achieve it.如果有任何工作代码可以实现它,请在这方面帮助我。

Thanks in Advance !!提前致谢 !!

I think I understand what you're asking.我想我明白你在问什么。

You want to create a single track using three tracks available to you.您想使用可用的三个轨道创建单个轨道。 Now, you cannot have audio files playing when you try to use them, they're locked.现在,当您尝试使用它们时无法播放音频文件,它们已被锁定。

You'll want to use AVMutableComposition, load all the tracks as AVURLAssets and then combine them.您需要使用 AVMutableComposition,将所有曲目作为 AVURLAssets 加载,然后将它们组合起来。 Now I've only written code to append one file to another file, so my example won't be complete, but it should point you in the right direction.现在我只编写了将一个文件附加到另一个文件的代码,所以我的示例并不完整,但它应该为您指明正确的方向。

// Generate a composition of the two audio assets that will be combined into
// a single track
AVMutableComposition* composition = [AVMutableComposition composition];
AVMutableCompositionTrack* audioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                 preferredTrackID:kCMPersistentTrackID_Invalid];

// grab the two audio assets as AVURLAssets according to the file paths
AVURLAsset* masterAsset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:self.masterFile] options:nil];
AVURLAsset* activeAsset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:self.newRecording] options:nil];

NSError* error = nil;

// grab the portion of interest from the master asset
[audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, masterAsset.duration)
                ofTrack:[[masterAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
                 atTime:kCMTimeZero
                  error:&error];
if (error)
{
    // report the error
    return;
}

// append the entirety of the active recording
[audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, activeAsset.duration)
                ofTrack:[[activeAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
                 atTime:masterAsset.duration
                  error:&error];

if (error)
{
    // report the error
    return;
}

// now export the two files
// create the export session
// no need for a retain here, the session will be retained by the
// completion handler since it is referenced there

AVAssetExportSession* exportSession = [AVAssetExportSession
                                   exportSessionWithAsset:composition
                                   presetName:AVAssetExportPresetAppleM4A];
if (nil == exportSession)
{
    // report the error
    return;
}


NSString* combined = @"combined file path";// create a new file for the combined file

// configure export session  output with all our parameters
exportSession.outputURL = [NSURL fileURLWithPath:combined]; // output path
exportSession.outputFileType = AVFileTypeAppleM4A; // output file type

[exportSession exportAsynchronouslyWithCompletionHandler:^{

    // export status changed, check to see if it's done, errored, waiting, etc
    switch (exportSession.status)
    {
        case AVAssetExportSessionStatusFailed:
            break;
        case AVAssetExportSessionStatusCompleted:
            break;
        case AVAssetExportSessionStatusWaiting:
            break;
        default:
            break;
    }
    NSError* error = nil;

    // your code for dealing with the now combined file
}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM