AVAudioPlayer从AVAudioRecorder播放后崩溃

时间:2010-04-30 05:10:34

标签: iphone avaudioplayer exc-bad-access avaudiorecorder

我有一个按钮,用户点击开始录制,然后再次点按停止。当它停止时我希望录制的声音“回声”回来,以便用户可以听到录制的内容。这第一次工作正常。如果我第三次按下按钮,它会开始一个新的录音,当我点击停止时,它会以EXC_BAD_ACCESS崩溃。

- (IBAction) readToMeTapped {

        if(recording)

        {
        recording = NO;
        [readToMeButton setTitle:@"Stop Recording" forState: UIControlStateNormal ];    

        NSMutableDictionary *recordSetting = [[NSDictionary alloc] initWithObjectsAndKeys:
         [NSNumber numberWithFloat: 44100.0],                 AVSampleRateKey,
         [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
         [NSNumber numberWithInt: 1],                         AVNumberOfChannelsKey,
         [NSNumber numberWithInt: AVAudioQualityMax],         AVEncoderAudioQualityKey,
         nil];

        // Create a new dated file
        NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
        NSString *caldate = [now description];          
        recordedTmpFile = [NSURL fileURLWithPath:[[NSString stringWithFormat:@"%@/%@.caf", DOCUMENTS_FOLDER, caldate] retain]];
        error = nil;
        recorder = [[ AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:&error];
        [recordSetting release];
        if(!recorder){
            NSLog(@"recorder: %@ %d %@", [error domain], [error code], [[error userInfo] description]);
            UIAlertView *alert =
            [[UIAlertView alloc] initWithTitle: @"Warning"
                                       message: [error localizedDescription]
                                      delegate: nil
                             cancelButtonTitle:@"OK"
                             otherButtonTitles:nil];
            [alert show];
            [alert release];
            return;
        }


        NSLog(@"Using File called: %@",recordedTmpFile);
        //Setup the recorder to use this file and record to it.

        [recorder setDelegate:self];
        [recorder prepareToRecord];

        [recorder recordForDuration:(NSTimeInterval) 5]; //recording for a limited time

    }
    else
    { // it crashes the second time it gets here!
        recording = YES;            
        NSLog(@"Recording YES Using File called: %@",recordedTmpFile);
        [readToMeButton setTitle:@"Start Recording" forState:UIControlStateNormal ];

        [recorder stop]; //Stop the recorder.

        //playback recording
        AVAudioPlayer * newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error];
        [recordedTmpFile release];

        self.aPlayer = newPlayer;
        [newPlayer release];

        [aPlayer setDelegate:self];
        [aPlayer prepareToPlay];
        [aPlayer play];
        }   
}

- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)sender successfully:(BOOL)flag {

        NSLog (@"audioRecorderDidFinishRecording:successfully:");

        [recorder release];
        recorder = nil;
}

检查调试器,它在此标记错误

@synthesize aPlayer, recorder;

这是我不明白的部分。我认为它可能与释放记忆有关,但我一直很小心。我错过了什么吗?

2 个答案:

答案 0 :(得分:1)

经过一段时间的努力,我偶然发现了debugging tip。它向我展示了AVAudioPlayer第二次被解除分配,造成了崩溃。那么代表必须做好清理工作吗?我检查了这个SO线程,并建议代表不会解除分配。但是,如果我删除该行

   [newPlayer release]; 

我的计划有效!在阅读了这个SO主题之后,我相信我的问题是我应该实现AVAudioPlayerDelegate's - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag方法并在播放完声音后释放音频播放器。我为AVAudioRecorder做过。

答案 1 :(得分:0)

您确定@property (retain)不包含非原子?我认为访问这些属性的一些代码可能是从另一个线程执行的。此外,您应该始终使用self.recorder和self.player通过其getter访问这些属性,以确保在各自的锁中访问它们。