【问题标题】:Resume recording after the AVAudioRecorder session is released释放 AVAudioRecorder 会话后恢复录制
【发布时间】:2013-03-06 06:15:33
【问题描述】:

我正在使用 AVAudioRecorder 类来录制声音。可以在一个 AVAudioRecorder 会话中开始、暂停、恢复、停止录制声音。如果应用程序关闭并重新启动,则无法恢复对已存在的音频文件的录制。

Issues with recording and playing .wav files IOS

请参考我的问题(以前在这里问过)来看看我的代码来初始化录音机。

有可能吗?请帮忙!谢谢!

【问题讨论】:

    标签: ios


    【解决方案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);
         }
    
     }
    
     ];
    }
    

    【讨论】:

    • 但是仅通过扩充已经存在的文件就不可能恢复录制吗?即使我也找不到可能,但如果有的话,不要错过。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    相关资源
    最近更新 更多