我正在使用AVAudioRecorder类录制声音。可以在一个AVAudioRecorder session 中开始,暂停,继续,停止录制声音。如果该应用程序关闭并再次启动,则无法将录制恢复到已经存在的音频文件。
Issues with recording and playing .wav files IOS
请参考我的问题(在此之前曾问过)以查看我的初始化录音机的代码。
有可能这样做吗?请帮忙!谢谢!
最佳答案
我认为您必须在每次关闭应用程序并将应用程序启动后将所有音频记录混合在一起时,将音频记录文件分别保存到文档目录中。
这里是用于混合音频文件的代码,在您的情况下,您只需要混合两个文件,因此需要相应地使用代码。
-(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);
}
}
];
}
关于ios - 释放AVAudioRecorder session 后继续记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15240288/