Reputation: 45
I am trying to create an application that allows someone to record and save their voice permanently in iOS5. That recording can then be played at any time. My code works as long as the user stays on the view that records and plays audio, but when I leave the view, come back to it, and try to play the audio, I get this error:
Error: The operation couldn’t be completed. (OSStatus error -50.)
Apparently, leaving the view causes the file saved in the Documents folder to not work somehow? Does anybody have a solution or answer to how I can fix this?
My code that records and plays audio is below:
- (IBAction)playSound:(id)sender {
if (!self.audioRecorder.recording)
{
NSError *error;
if (self.audioPlayer)
self.audioPlayer = nil;
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:self.audioRecorder.url
error:&error];
self.audioPlayer.delegate = self;
[self.audioPlayer prepareToPlay];
if (error)
NSLog(@"Error: %@",
[error localizedDescription]);
else
[self.audioPlayer play];
}
}
- (IBAction)recordSound:(id)sender {
if(!self.audioRecorder.recording)
{
self.dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
self.docsDir = [self.dirPaths objectAtIndex:0];
self.soundFilePath = [self.docsDir
stringByAppendingPathComponent:@"sound.caf"];
self.soundFileURL = [NSURL fileURLWithPath:self.soundFilePath];
self.recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
nil];
NSError *error = nil;
self.audioRecorder = [[AVAudioRecorder alloc]
initWithURL:self.soundFileURL
settings:self.recordSettings
error:&error];
if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
} else {
[self.audioRecorder prepareToRecord];
}
[self.audioRecorder record];
[self.audioRecorder recordForDuration:(NSTimeInterval) 5];
[self.record setTitle:@"Stop" forState: UIControlStateNormal];
}
else if (self.audioRecorder.recording)
{
[self.audioRecorder stop];
[self.record setTitle:@"Record" forState: UIControlStateNormal];
}
else if (self.audioPlayer.playing) {
[self.audioPlayer stop];
}
}
Upvotes: 0
Views: 480
Reputation: 8718
I'm guessing it happens because when you record the file, you specify a file name, and the url is saved in the self.audioRecorder.url
property.
Once you leave the view, the AVAudioPlayer
object self.audioPlayer
and self.audioRecorder
are released from the memory.
When you come back, trying to initalize it with the property self.audioRecorder.url
it doesn't really exists, therefore initializes with nil.
Try changing you alloc and init from this:
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:self.audioRecorder.url
error:&error];
to this:
self.dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
self.docsDir = [self.dirPaths objectAtIndex:0];
self.soundFilePath = [self.docsDir
stringByAppendingPathComponent:@"sound.caf"];
self.soundFileURL = [NSURL fileURLWithPath:self.soundFilePath];
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:self.soundFileURL
error:&error];
Or something similar, as long as you initialize the player with the file stored in the file system. not from the object stored in the memory
Upvotes: 1