我有一个按钮,用户点击开始录制,然后再次点击停止。当它停止时,我想要录制的语音“回声”回来,以便用户可以听到所录制的内容。这是第一次正常工作。如果我第三次按下按钮,它会开始一个新的录音,当我停下来时,它会与EXC_BAD_ACCESS一起崩溃。AVAudioPlayer从AVAudioRecorder播放后崩溃
- (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;
错误这是我不理解的部分。我认为这可能与释放记忆有关,但我一直很小心。我错过了什么吗?
您对多线程编程有多熟悉? – lucius 2010-04-30 07:23:11