AvAudioRecorder 录音文件随机损坏

Posted

技术标签:

【中文标题】AvAudioRecorder 录音文件随机损坏【英文标题】:AvAudioRecorder recording file corrupted randomly 【发布时间】:2012-10-12 12:45:35 【问题描述】:

我遇到了 AvAudioRecorder 的问题,随机检查创建损坏的文件。

它创建长度为 24kb 的文件,并非一直发生,而是随机找到的文件已损坏。

我还检查了下面的链接以获取参考,因为我有类似的问题,但它对我不起作用。

AVAudioRecorder Record method returns NO at random times

Seemingly random file corruption using AVAudioRecorder (Sometimes the file can't be played back) - ios

下面是完整的功能代码。

-(IBAction)btnRecordClick:(id)sender

    [PlayingTimer invalidate];
    PlayingTimer = nil;

    if(isPaused==NO)
    
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        NSError *err = nil;
        [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];

        if(err)
        
            // NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
            return;
        

        [audioSession setActive:YES error:&err];
        err = nil;

        if(err)
        
            // NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
            return;
        

        //*******Working well with .wav,.caf and .mp4 files *****************
        recordSetting = [[NSMutableDictionary alloc] init];

        //this is for .wav and .caf file
        //this is for .mp4 file for iPhone 3GS/iPhone 4
        [recordSetting setValue: [NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
        [recordSetting setValue: [NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
        [recordSetting setValue: [NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
        [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
        [recordSetting setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
        [recordSetting setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];

        self.strMessage = @"";
        self.strMessageType = [NSString stringWithFormat:@"Audio Message"];
        self.strFileName = [NSString stringWithFormat:@"%@.mp4", [_globalmodel stringUniqueID]];
        // NSLog(@"FileName :%@",self.strFileName);

        self.strUniqueRef = [NSString stringWithFormat:@"%@",[_globalmodel stringUniqueID]];

        NSString *soundPath;//=[NSString alloc];
        soundPath = [NSString stringWithFormat:@"%@/%@", DOCUMENTS_FOLDER,self.strFileName] ;
        NSURL *url1 = [NSURL fileURLWithPath:soundPath];

        // NSLog(@"Recording Path:%@",url1);
        err = nil;
        recorder = [[ AVAudioRecorder alloc] initWithURL:url1 settings:recordSetting error:&err];

        if(!recorder)
        
            // NSLog(@"recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
            UIAlertView *alert =[[UIAlertView alloc] initWithTitle: @"My Easy Media"
                                                           message: @"The Recording format is not supported on your device!"
                                                          delegate: nil
                                                 cancelButtonTitle:@"OK"
                                                 otherButtonTitles:nil];
            [alert show];
            [alert release];

            return;
        

        //prepare to record
        [recorder setDelegate:self];
        [recorder prepareToRecord];
        recorder.meteringEnabled = YES;
        BOOL audioHWAvailable = audioSession.inputIsAvailable;

        if (! audioHWAvailable)
        
            UIAlertView *cantRecordAlert = [[UIAlertView alloc] initWithTitle: @"Warning"
                                                                      message: @"Audio input hardware not available"
                                                                     delegate: nil
                                                            cancelButtonTitle:@"OK"
                                                            otherButtonTitles:nil];
            [cantRecordAlert show];
            [cantRecordAlert release];

            return;
        

        // start recording
        [recorder record];
        _isRecordingStarted = YES;

        // SET TIMER // START TIMER
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(EndRecording:) userInfo:nil repeats:YES];

        // END TO SETTING TIMER
        timerDisplayRight.hidden = NO;
        timerDisplayLeft.hidden = NO;
        timerDisplayRight.text = [NSString stringWithFormat:@"%02d:%02d", intMimute,-- intSecond]; 
        timerDisplayLeft.text = [NSString stringWithFormat:@"00:00"];
        progressView.progress = 0.0;
    
    else
    
        isPaused=NO;
        [recorder record];   
        _isRecordingStarted = YES;
        [btnReview setEnabled:NO];

        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(EndRecording:) userInfo:nil repeats:YES];
        timerDisplayRight.hidden = NO;
        timerDisplayLeft.hidden = NO;
    

    timerDisplayRight.text = [NSString stringWithFormat:@"%02d:%02d", intMimute,-- intSecond];



-(IBAction)btnPauseClick:(id)sender

    // NSLog(@"Pause Button of ActionSheet Clicked");
    [timer invalidate];
    timer = nil;
    isPaused=YES;
    [recorder pause];

    [btnPause setEnabled:NO];
    [btnRecord setEnabled:YES];
    [btnStop setEnabled:YES];



-(IBAction)btnStopClick:(id)sender

    // NSLog(@"Stop Button of ActionSheet Clicked");
    [recorder stop];

    progressView.progress = 1.0;
    // NSLog(@"Total Recording Secomd : %d", totalRecordingSecond);
    finalRecordingSecond = totalRecordingSecond;

    [btnStop setEnabled:NO];
    [btnReview setEnabled:YES];
    [btnRecord setEnabled:YES];
    [btnPause setEnabled:NO];

    timerDisplayRight.text = [NSString stringWithFormat:@"%02d:%02d", (totalRecordingSecond/60), (totalRecordingSecond % 60) ];
    timerDisplayLeft.text = [NSString stringWithFormat:@"00:00"];

    [timer invalidate];
    timer = nil;

    // REINITIALIZE TIMER VARIABLES...
    intMimute =  5;
    intTotalTime = (60 * intMimute);   
    intSecond = intTotalTime / intMimute;
    intMimute--;       

    // REINITIALIZE TIMER VARIABLES...       
    _isRecordingStarted = NO;

请帮忙。一旦我开始录制,大约 10 到 15 秒后,点击停止。

【问题讨论】:

找到原因了吗?你是怎么解决的? 【参考方案1】:

我认为当任何人在单独的线程中在后台运行任何进程时会发生这种情况,同时在录制时。

【讨论】:

请确认是问题所在,或者您认为是问题所在。当我在 15-20 个文件中录制音频时,我遇到了同样的问题 1-2 个文件损坏了

以上是关于AvAudioRecorder 录音文件随机损坏的主要内容,如果未能解决你的问题,请参考以下文章

IOS 录音(AVAudioRecorder)

AVAudioRecorder / AVAudioPlayer - 将录音附加到文件

AVAudioRecorder - 最后裁剪/修剪录音

AVAudioRecorder 设置录音开始时间

iPhone - 使用 AVAudioRecorder 录音

使用AVAudioRecorder录制音频