iOS - Playback of recorded audio fails with OSStatus error -43 (file not found)
avaudioplayer, avaudiorecorder, avfoundation, core-audio, ios
Solution
I'm gonna take a wild stab at this since no-one else has answered (EDIT: now there is) (I don't have much experience with Audio in iOS) but...
Try changing
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];
to
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
Problem
I set up an AVAudioRecorder instance the following way when my view loads: ``` AVAudioSession *audioSession = [AVAudioSession sharedInstance]; audioSession.delegate = self; [audioSession setActive:YES error:nil]; [audioSession setCategory:AVAudioSessionCategoryRecord error:nil]; NSString *tempDir = NSTemporaryDirectory(); NSString *soundFilePath = [tempDir stringByAppendingPathComponent:@"sound.m4a"]; NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; NSLog(@"%@", soundFileURL); NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:kAudioFormatMPEG4AAC], AVFormatIDKey, [NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey, [NSNumber numberWithInt:16], AVEncoderBitRateKey, [NSNumber numberWithInt: 1], AVNumberOfChannelsKey, [NSNumber numberWithFloat:8000.0], AVSampleRateKey, [NSNumber numberWithInt:8], AVLinearPCMBitDepthKey, nil]; NSError *error = nil; self.recorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL settings:recordSettings error:&error]; self.recorder.delegate = self; if (error) { NSLog(@"error: %@", [error localizedDescription]); } else { [self.recorder prepareToRecord]; } ``` Then, when the user presses the record button I do: ``` - (IBAction)record:(id)sender { if (!self.isRecording) { self.isRecording = YES; [self.recorder record]; self.recordButton.enabled = NO; } } ``` Then to stop the recording/playback: ``` - (IBAction)stop:(id)sender { if (self.isRecording) { [self.recorder stop]; self.recordButton.enabled = YES; self.isRecording = NO; } if (self.isPlaying) { [self.player stop]; self.isPlaying = NO; self.playButton.enabled = YES; } } ``` After, I want to be able to playback what was recorded, so I do the following: ``` - (IBAction)play:(id)sender { NSError *error = nil; self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:self.recorder.url error:&error]; self.player.delegate = self; if (error) { NSLog(@"%@", error.localizedDescription); } else { self.isPlaying = YES; [self.player play]; self.playButton.enabled = NO; } } ``` But when I go and play the file is get OSStatus error -43 file not found. This is all running on the device btw, on the simulator y an error when trying to instantiate the recorder or player, and from I've seen it's a simulator issue. EDIT 1: I solved the OSStatus error -43 part it has something to do with the encoding format, I commented out the format and it recorded and played the file properly, but I need to record in a compressed format. Does anyone know the settings for this? EDIT 2: I managed to find settings that worked for compressed AAC audio. A 2min audio file comes out to 256KB, I updated my code to reflect the current state. Thanks to all of those who answered for your help.