如何通过在 iphone 中混合多个音频来录制音频?
Posted
技术标签:
【中文标题】如何通过在 iphone 中混合多个音频来录制音频?【英文标题】:How to record an audio by mixing multiple audio in iphone? 【发布时间】:2012-12-03 15:24:37 【问题描述】:我必须通过混合多个音频文件来录制音频。例如,如果正在播放三个音频文件,那么我必须将所有播放音频的声音混合并将其录制到一个音频文件中。 如果有任何工作代码可以实现它,请在这方面帮助我。
提前致谢!!
【问题讨论】:
你有什么起点吗?我觉得这个问题太笼统了 What have you tried? 【参考方案1】:我想我明白你在问什么。
您想使用三个可用的轨道创建一个轨道。现在,您在尝试使用音频文件时无法播放它们,它们已被锁定。
您需要使用 AVMutableComposition,将所有曲目加载为 AVURLAsset,然后将它们组合起来。现在我只编写了将一个文件附加到另一个文件的代码,所以我的示例并不完整,但它应该为您指明正确的方向。
// 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
];
【讨论】:
以上是关于如何通过在 iphone 中混合多个音频来录制音频?的主要内容,如果未能解决你的问题,请参考以下文章