How To: iPhone SDK – Play and Record Audio concurrently
Unfortunately, it’s quite a fiddly process to record audio and play it back at the same time on the iPhone. By default, the sound output is very quiet from the iPhone’s speaker when you are recording sound. So how do we fix this?
First, setup your audio session to record audio:
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];
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
if (recorder) {
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
} else {
NSLog(@"Error: %@", error);
}
Then tell the device you want to record and play audio at the same time:
AVAudioSession *audioSession = [AVAudioSession sharedInstance]; NSError *err = nil; [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
Then, and this is the key, allow the volume from the speakers to also be loud when recording:
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride),&audioRouteOverride);
The end result? You can happily play audio at full volume while recording from the device’s microphone. Happy days.

May 6th, 2010 at 2:21 am
Hi.
Thanks lot for for this post. The last 3 lines of code were really useful.
I also wanted to know if there is a way to record what you are playing without using the mic input.
For example if I am listing to a radio stream, is it possible to record the audio?
Thnaks again for the post
Ed
June 18th, 2010 at 3:48 am
Thanks!!
This is very useful!!
Thanks a lot!!
July 25th, 2010 at 7:43 am
/dev/null PROBLEM!
If you analize the app in Instruments (with IO and Memory module) then you can see that memory just keeps growing. (dev/NULL write operations)
For example: if you are metering audio leveles every 1 second.
How can I solve this?
July 25th, 2010 at 7:45 am
Ed:
Apple doesnt wants you to record any audio due to copy rights, but TuneIn Radio app has such feature. So, dunno the real thing.
July 25th, 2010 at 7:57 am
Or is it just me leaking with Data?
August 23rd, 2010 at 6:56 pm
I have tried to use this but failed. What i am actually trying to do is to playback the same what is being recorded simultaneously. No output is got at the speaker. I have tried with all the remaining SDKs but no result