Take the 2-minute tour ×

I'm having problems with AVAudioSession using the AVAudioRecorder in a cocos2d game that I'm working on.

I'm trying to capture mic input using a simple AVAudioRecorder example to detect when the user makes a sound in the mic (the sound itself doesn't matter, as I'm recording into /dev/null).

Here is my setup code for the microphone:

    NSURL *newURL = [[NSURL alloc] initFileURLWithPath:@"/dev/null"];


    NSDictionary *recordSettings =
    [[NSDictionary alloc] initWithObjectsAndKeys:
     [NSNumber numberWithFloat: 22050.0], AVSampleRateKey,
     [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
     [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
     [NSNumber numberWithInt: AVAudioQualityLow],

    micInput = [[AVAudioRecorder alloc] initWithURL:newURL settings:recordSettings error:nil];
    [newURL release];
    [recordSettings release];
    [micInput setMeteringEnabled:YES];

On the iPhone with the above code, the scene starts with all of the audio (sound effects, background music, etc.) playing at a really low level, because it is only playing through the phone speaker instead of the external speaker. When I test this on iPad or iPod Touch, the background audio plays through the external speaker as expected. This is a problem, since the volume of the game lowers drastically when playing on the iPhone version during this particular part of the game.

When I comment out the AVAudioSession setup line, the sounds play through the external speaker, but of course I can't get microphone input anymore. Is there any workaround or solution to this problem? I need to be able to record with AVAudioRecorder but still have audio output from the iPhone's external speaker.


share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Try something like the following after you setup your audio session:

         UInt32 ASRoute = kAudioSessionOverrideAudioRoute_Speaker;
         AudioSessionSetProperty (
           sizeof (ASRoute),
share|improve this answer
this solved my problem. Thanks very much for your help! –  chmod Jan 25 '11 at 16:19
add comment

Your Answer


