iOS - Overwrite the particular audio recording In specific time - iphone

Hi I need to develop an app that can record, play, stop and overwrite audio. I have done the Recording by using AvAudioRecorder:
NSDictionary *audioSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100],AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatLinearPCM],AVFormatIDKey,
[NSNumber numberWithInt: 2],AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityLow], AVEncoderAudioQualityKey,
nil];
self.audioRecorder = [[AVAudioRecorder alloc]
initWithURL:audioFileURL
settings:audioSettings
error:nil];
[sliderTimer invalidate];
[self.audioRecorder record];
sliderTimer = [NSTimer scheduledTimerWithTimeInterval:0.2
target:self
selector:#selector(updateSlider)
userInfo:nil repeats:YES];
...and playback is done using AVplayer.
But I dont know how to overwrite the recording. This the outline of my overwrite implementation:
Stop Recording
Move the slider position to the particular point
Then, start recording.
This is the functionality to overwrite the previous recordings.
So, As per the steps I have written
[self.audioRecorder stop];
[[self audioSlider] setValue:self.audioSlider.value animated:YES];
[self.audioRecorder recordAtTime:self.audioSlider.value forDuration:self.audioSlider.maximumValue];
...but its not working. Instead, they totally re-record the file. Could any body help me for this critical situation.

Create audioSession object,its upto you whether you want to activate or deactivate your app’s audio session.(AVAudioSession)
audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
if(err) {
NSLog(#"audioSession: %# %d %#", [err domain], [err code], [[err userInfo] description]);
return;
}
[audioSession setActive:YES error:&err];
Start recording proces,
-startRecording
{
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
// We can use 44100, 32000, 24000, 16000 or 12000 depending on sound quality
[recordSetting setValue:[NSNumber numberWithFloat:32000.0] forKey:AVSampleRateKey];
// We can use 2(if using additional h/w) or 1 (iPhone only has one microphone)
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
mediaPath = [[NSString stringWithFormat:#"%#/myVoice.mp3", DOCUMENTS_FOLDER] retain];//where you want to save your recorded audio.
NSURL *url = [NSURL fileURLWithPath:mediaPath];
err = nil;
NSData *audioData = [NSData dataWithContentsOfFile:[url path] options: 0 error:&err];
recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
[recorder setDelegate:self];
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
BOOL audioHWAvailable = audioSession.inputAvailable;
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;
}
[recorder record];
}
Pause recording:
-pauseRecording
{
[recorder pause];
//[recorder updateMeters];
}
Again resume the process..audiosession will do it for you...
-resumerecording
{
[recorder record];
//[recorder updateMeters];
}
EDIT: [recorder updateMeters]; should be called periodically which refreshes the average and peak power values for all channels of an audio recorder.
You can use timer for that.
For example:
NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(updateAudioDisplay) userInfo:NULL repeats:YES];
-(void)updateAudioDisplay
{
if (!recorder.isRecording)
{
[recorder updateMeters];
}
else
{
if (recorder == nil) {
//recording is not yet started
}
else
{
//paused
}
}
}
You can download the sample code from here.

Related

MPAVController >> Not enough buffered to keep up

I am playing video using this code.
player= [[ MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
player.navigationController.navigationBar.hidden = YES;
player.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
player.moviePlayer.controlStyle = MPMovieControlStyleNone;
player.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
player.moviePlayer.fullscreen = NO;
[self presentModalViewController:player animated:NO];
Video playing works perfectly But the problem is that after finishing play i am getting this result in console.
[MPAVController] Autoplay: Likely to keep up or full buffer: 0
[MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up.
and then after i am trying to record the voice but unable to record.
can anybody help me to solve this issue.
same kind of task is done by me... you can take refernce of my code... i hope it helps...
- (IBAction) startRecording:(id)sender
{
NSLog(#"**************Recording start**************");
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
// We can use kAudioFormatAppleIMA4 (4:1 compression) or kAudioFormatLinearPCM for nocompression
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
// We can use 44100, 32000, 24000, 16000 or 12000 depending on sound quality
[recordSetting setValue:[NSNumber numberWithFloat:41000.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: kAudioFormatAppleLossless] forKey:AVFormatIDKey];
// We can use 2(if using additional h/w) or 1 (iPhone only has one microphone)
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
[recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVEncoderBitRateKey];
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:mediaPath])
{
[[NSFileManager defaultManager] createDirectoryAtPath:mediaPath withIntermediateDirectories:NO attributes:nil error:&error];
}
mediaPath = [[NSString stringWithFormat:#"%#/sound.caf", DOCUMENTS_FOLDER] retain];
NSURL *url = [NSURL fileURLWithPath:mediaPath];
NSLog(#"AUDIOPATH%#",mediaPath);
err = nil;
NSData *audioData = [NSData dataWithContentsOfFile:[url path] options: 0 error:&err];
if(audioData) {
NSFileManager *fm = [NSFileManager defaultManager];
[fm removeItemAtPath:[url path] error:&err];
}
err = nil;
audioRecorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
if(!audioRecorder){
NSLog(#"audioRecorder : %# %d %#", [err domain], [err code], [[err userInfo] description]);
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle: #"Warning"
message: [err localizedDescription]
delegate: nil cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
return;
}
//prepare to record
[audioRecorder setDelegate:self];
[audioRecorder prepareToRecord];
audioRecorder.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
[audioRecorder record];
}
I am not sure if this will help but you should be presenting your MPMoviePlayerViewController with presentMoviePlayerViewControllerAnimated:, not presentModalViewController:animated:, like so:
[self presentMoviePlayerViewControllerAnimated:player];
If you are manually dismissing the MPMoviePlayerViewController you should use dismissMoviePlayerViewControllerAnimated.
Here is a thread that might help solve the issue with the MPAVController logs. It seems like you are doing a lot of customization of the MPMoviePlayerViewController moviePlayer -- you might honestly consider just using an MPMoviePlayerController instead of an MPMoviePlayerViewController.
Without seeing the code you are using to perform voice recording it is impossible to tell whether your voice recording problems are stemming from something to do with MPMoviePlayerViewController or if you have an error elsewhere. I would suggest posting more of your code.
I also found that these messages can be caused by sending an incorrect URL to the media player. Sometimes it really is that simple.
You need to record audio after playing video,so you have to use this notification to know that movie player has completed playing video...For this u need to use this code.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
In that moviePlaybackComplete: method you have record audio.
For this purpose you better to use AVAudioPlayer class.
Now use this piece of code for recording..
AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:&error];
[recorder setDelegate:self];
[recorder prepareToRecord];
[recorder record];
Check the useApplicationAudioSession property on MPMoviePlayerController. In iOS 3.1.x, the movie player was always getting its own system provided audio session. In newer version of iOS , it can now share the application session, and that's the default value as well. Try switching that property to NO before starting to play your movie.

Objective C: auto play after recording with specific time

Good day, can you help me with my project in making a recording project, which is after you recorded it using AVAudioRecorder it will automatically play in a certain time can you give me or site me a link regarding with my question..i am badly needed your help masters, because i'm new at iOS development. thanks in advance guys. have a good day.
here's my code # startrecording:
-(void)startRecording:(UIButton *)sender
{ //for recording
recStopBtn.enabled=NO;
recStopBtn.hidden = NO;
recStopBtn.enabled =YES;
playRecBtn.enabled = NO;
loading.hidden = NO;
[loading startAnimating];
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;
}
recordSetting = [[NSMutableDictionary alloc] init];
// We can use kAudioFormatAppleIMA4 (4:1 compression) or kAudioFormatLinearPCM for nocompression
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
// We can use 44100, 32000, 24000, 16000 or 12000 depending on sound quality
[recordSetting setValue:[NSNumber numberWithFloat:16000.0] forKey:AVSampleRateKey];
// We can use 2(if using additional h/w) or 1 (iPhone only has one microphone)
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
// These settings are used if we are using kAudioFormatLinearPCM format
//[recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
//[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
//[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
recorderFilePath = [NSString stringWithFormat:#"%#/MySound.caf", DOCUMENTS_FOLDER];
NSLog(#"recorderFilePath: %#",recorderFilePath);
NSURL *url = [NSURL fileURLWithPath:recorderFilePath];
err = nil;
NSData *audioData = [NSData dataWithContentsOfFile:[url path] options: 0 error:&err];
if(audioData)
{
NSFileManager *fm = [NSFileManager defaultManager];
[fm removeItemAtPath:[url path] error:&err];
}
err = nil;
recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
if(!recorder){
NSLog(#"recorder: %# %d %#", [err domain], [err code], [[err userInfo] description]);
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle: #"Warning"
message: [err localizedDescription]
delegate: nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
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];
return;
}
// start recording
[recorder record];
lblStatusMsg.text = #"Recording...";
NSLog(#"RECORDING");
//recIcon.image = [UIImage imageNamed:#"rec_icon.png"];
//progressView.progress = 0.0;
//timer = [NSTimer scheduledTimerWithTimeInterval:6.0 target:self selector:#selector(handleTimer) userInfo:nil repeats:YES];
}
If all you need is how to make something happen after a certain amount of time, use an NSTimer. I see you already have one commented out in your code.
To record for a certain amount of time use recordForDuration. To stop recording manually use stop. To play a recording use the url property of the AVAudioRecorder in AVAudioPlayer method initWithContentsOfURL.
So basically, uncomment your NSTimer and then do
AVAudioPlayer *player;
- (void) handleTimer
{
player = [[AVAudioPlayer alloc] initWithContentsOfURL:nameOfAudioRecorder.url];
}

How can i record a song play from iPod library using MPMediaPickerController?

I am making an App in which i have to play music from iPod Music Library using MPMediaPickerController.After playing a song i want to record the song with some external Voice(For example:- User's Voice).I m trying to do the same but getting a problem i.e.when App launches firstly,i choose a song from iPod Music library after that i click on start Recording Button.when i click on Start Recording button my song which i played before stops but the recording is working properly.Recording of User's Voice is working fine but song is not getting recorded as i told that song stops when "Start Recording" button clicked.I am using AVAudioRecorder for Recording.This is my code which i m using.
-(Void)ViewDidLoad
{
self.musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
}
- (void)playOrPauseMusic:(id)sender {
MPMusicPlaybackState playbackState = self.musicPlayer.playbackState;
if (playbackState == MPMusicPlaybackStateStopped || playbackState == MPMusicPlaybackStatePaused) {
[self.musicPlayer play];
} else if (playbackState == MPMusicPlaybackStatePlaying) {
[self.musicPlayer pause];
}
}
- (void)openMediaPicker:(id)sender {
MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
mediaPicker.delegate = self;
mediaPicker.allowsPickingMultipleItems = NO; // this is the default
[mediaPicker shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeRight];
[self presentModalViewController:mediaPicker animated:YES];
}
- (void) startRecording
{
[self.musicPlayer play];
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;
}
recordSetting = [[NSMutableDictionary alloc] init];
// We can use kAudioFormatAppleIMA4 (4:1 compression) or kAudioFormatLinearPCM for nocompression
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
// We can use 44100, 32000, 24000, 16000 or 12000 depending on sound quality
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
// We can use 2(if using additional h/w) or 1 (iPhone only has one microphone)
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
[recordSetting setObject:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey];
[recordSetting setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setObject:[NSNumber numberWithInt: AVAudioQualityMax] forKey: AVEncoderAudioQualityKey];
recorderFilePath = [NSString stringWithFormat:#"%#/MySound.caf", DOCUMENTS_FOLDER] ;
NSLog(#"recorderFilePath: %#",recorderFilePath);
NSURL *url = [NSURL fileURLWithPath:recorderFilePath];
err = nil;
NSData *audioData = [NSData dataWithContentsOfFile:[url path] options: 0 error:&err];
if(audioData)
{
NSFileManager *fm = [NSFileManager defaultManager];
[fm removeItemAtPath:[url path] error:&err];
}
err = nil;
recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
if(!recorder){
NSLog(#"recorder: %# %d %#", [err domain], [err code], [[err userInfo] description]);
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle: #"Warning"
message: [err localizedDescription]
delegate: nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
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];
return;
}
// start recording
[recorder recordForDuration:(NSTimeInterval) 2];
lblStatusMsg.text = #"Recording...";
selector:#selector(handleTimer) userInfo:nil repeats:YES];
}
This is my Code of MPMusicPlayer and AVAudioRecorder.Please help.Thanks in advance.
You can't record Songs imported from iPod library, you can only record mp3 file, for that you need to convert Mediaitem into mp3 format.
you can get referance for that from here, i hope this may help you.
Maybe look into playing two tracks simultaneously. You could record a separate voice over track and trigger this and your mp3 to start playing at the same time. Obviously this would only work inside your app.

Differences in behavior of AVAudioRecorder in iOS 4.1 and iOS3.1.3

I want to play an audio when someone speaks in the iPhone's mic. This code works perfectly in iPhone 2G(OS 3.1.3), but when it is tested in iPhone 4, it doesnt work. I checked for API and method differences, but could not find any.
- (void)viewDidLoad {
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/Audio.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.delegate=self;
if (audioPlayer == nil)
NSLog(#" %# ", [error description]);
audioSession = [[AVAudioSession sharedInstance] retain];
[audioSession setActive:YES error: nil];
[super viewDidLoad]; }
The action event:
-(IBAction) micAction{
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: nil];
NSURL *url = [NSURL fileURLWithPath:#"/dev/null"];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
NSError *error;
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
if (recorder) {
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: #selector(levelTimerCallback:) userInfo: nil repeats: NO];
} }
- (void)levelTimerCallback:(NSTimer *)t {
[recorder updateMeters];
if([recorder averagePowerForChannel:0]>-38){
if ([audioPlayer prepareToPlay]) {
[recorder stop];
[recorder release];
[levelTimer invalidate];
[self playSound];
}
}}
-(void)playSound{
[audioSession setCategory:AVAudioSessionCategoryPlayback error: nil];
if ([audioPlayer prepareToPlay])
[audioPlayer play]; }
To make sure that the audio is playing in iPhone 4, i'd also set up an IBAction event, which is working fine. Also, for testing I'd set up a label to display the value of
[recorder averagePowerForChannel:0
after the button is pressed. This label updates itself as per the timer in 2G, but in iPhone 4 it displays just one value on the click event.
Am unable to debug the behavior of the method in iPhone 4 since i dont have the device with me now.
Can anyone please point out what the error is?
Thanks
Have you tried saving to a file instead of to /dev/null?
Found the solution. Issue was in this line:
levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: #selector(levelTimerCallback:) userInfo: nil repeats: NO];
Since repeats was set to NO, the timer used to fire only once. So, the continuos monitoring of the averagePowerForChannel value could not be done. After setting it to YES, the code works perfectly.

AVAudioPlayer crash after playing from an AVAudioRecorder

I've got a button the user tap to start recording and tap again to stop. When it stop I want the recorded voice 'echo' back so the user can hear what was recorded. This works fine the first time. If I hit the button for the third time, it starts a new recording and when I hit stop it crashes 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;
}
Checking the debugger, it flags the error here
#synthesize aPlayer, recorder;
This is the part I don't understand. I thought it may have something to do with releasing memory but I've been careful. Have I missed something?
After working on it for a while I stumbled on this debugging tip. It showed me that AVAudioPlayer was deallocated the second time around, causing the crash. So the Delegate must have done the clean up? I checked this SO thread and it is suggestion that the Delegate does not deallocate. However, if I remove the line
[newPlayer release];
My program works! After reading this SO thread, I believe my issue is that I should implement AVAudioPlayerDelegate's - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag method and release the audio player there, after the sound is done playing. I had done it for AVAudioRecorder.
Did you make sure that #property (retain) does not include nonatomic? I'm thinking that some of the code that accesses those properties may be doing so from another thread. Also, you should always access those properties via their getters by using self.recorder and self.player, in order to make sure they are accessed within their respective locks.