我正在使用AVAudioRecorder类来录制声音。可以在一个AVAudioRecorder会话中启动,暂停,恢复,停止录制声音。如果应用程序关闭并重新启动,则无法将录制内容恢复为已存在的音频文件。
Issues with recording and playing .wav files IOS
请参考我的问题(此处曾提到过)查看我的代码以初始化录音机。
有可能这样做吗?请帮忙!谢谢!
答案 0 :(得分:1)
我认为您每次关闭应用程序时都需要将录音文件单独保存到文档目录中,并且在应用程序启动后将所有录音混合为可用。
这里是混合音频文件的代码,在你的情况下你只需要混合两个文件,因此需要相应地使用代码。
-(void)mixAudios
{
//mix all audio files
AVMutableComposition* mixComposition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
for (number of your recordings)
{
AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:[NSURL URLWithString:@"** Recording file Path **"] options:nil];
if(![compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:CMTimeMake(Start time in seconds for each Recoring,1) error:nil])
{
NSLog(@" error: %@"error);
}
}
//save mixed composition as file
AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition
presetName:AVAssetExportPresetPassthrough];
NSString* videoName = @".mov";
NSString* videoPath = [[NSString alloc] initWithFormat:@"%@/%@", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0], videoName];
NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName];
if([[NSFileManager defaultManager] fileExistsAtPath:exportPath])
{
[[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
}
NSURL *exportUrl = [NSURL fileURLWithPath:exportPath];
_assetExport.outputFileType = @"com.apple.quicktime-movie";
_assetExport.outputURL = exportUrl;
_assetExport.shouldOptimizeForNetworkUse = YES;
[_assetExport exportAsynchronouslyWithCompletionHandler:
^(void )
{
[_assetExport release];
NSString *path = [NSString stringWithString:[exportUrl path]];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (path)) {
UISaveVideoAtPathToSavedPhotosAlbum (path, nil, nil, nil);
}
}
];
}