AVAudioPlayer crashes after playing with AVAudioRecorder

I have a button that the user clicks to start recording and click again to stop. When it stops, I want the recorded "echo" voice to return so the user can hear the recorded one. This works great the first time. If I pressed the button a third time, it starts a new recording and when I click, stop it with 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;
}

      

While checking the debugger it puts the error here

@synthesize aPlayer, recorder;

      

This is the part I don't understand. I thought it might have something to do with memory release, but I was careful. Did I miss something?

+2


a source to share


2 answers


After working on this for a while, I came across this debugger . It showed me that AVAudioPlayer was released a second time, causing a crash. So the delegate must have done the cleanup? I have checked this fooobar.com/questions/2584400 / ... thread and this is a suggestion that Delegate does not free. However, if I remove the line

   [newPlayer release]; 

      



My program works! After reading this fooobar.com/questions/2584400 / ... thread, I believe my problem is that I have to implement a method AVAudioPlayerDelegate - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag

and free the audio player there, after the sound has been played. I did this for AVAudioRecorder.

+1


a source


Are you sure it @property (retain)

doesn't include non-atomic? I think some of the code that accesses these properties might be doing it from a different thread. Also, you should always access these properties through your getters using self.recorder and self.player to ensure that they are available in their respective locks.



0


a source







All Articles