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.
You may need to set the AVAudioSession category back to playback mode:
This should also avoid the problem where the audio will not play with the mute switch toggled on.
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
to
Okay try this:
Make
NSURL *soundFileURL
global for your class so you can access it anywhere in your class and set in the same way as you are now (except it would besoundFileURL = [NSURL fileURLWithPath:soundFilePath];
instead ofNSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
.Then change this
to this
AVEncoderBitRateKey
is not working withAAC
format encoding.So try this code.EDIT 1:
OSStatus error -43 file not found is not a simulator issue. It's an error log, which shows your
recording settings are not correct
and requiredencoding format is not supported for the settings you have selected
.When you get this error, you file will get created in the format as you given. But It will
not initialize your audio recorder to start recording
. If you have any doubt, then select the same settings, which you had in first question and record and try to play that. It won't record and play because of unsupported setting.So for a
compressed encoding format
, you can use a settings as I given above in my code. You can have.aac
format for this settings. It will take164KB per 10 seconds to record in aac
format. For rest of the formats you have to try and figure out.