8
votes

i have a problem with AVAudioRecorder and AVAudioPlayer.

when i use Player and Record at the same time (eg. for playing sound while recording) the sound is in the quiet internal Speaker. i searched stackoverflow and all i found was this code:

UInt32 *audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

But this doesn't help me :( When i copyPaste it, i got errors. What can i do to record and play the loud Speaker at the bottom?

I don't use anything like SCLister oder something...

Thanks in advance

Max

6

6 Answers

16
votes

This is a bit old, but this post helped me and I wanted to update it for anyone else who might need it in the future. The code posted at the top is correct - it will take the quiet audio that's being played through the phone speaker and route it to the loudspeaker at the bottom. There is a minor typo in the code, which is why it's giving errors. Here is the correct snippet which will solve this issue:

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

Make sure you also activate the audio session right after setting this, before creating your audio player/recorder:

[[AVAudioSession sharedInstance] setActive:YES error:nil];

Last, if you're going to be playing and recording at the same time you'll probably need to set the category and mixing functions too. Here's the entire snippet which will set the category, enable mixing, route the audio to the main speaker, and activate the session. You'll want to do this only once right after the app launches.

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

OSStatus propertySetError = 0;
UInt32 allowMixing = true;
propertySetError = AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixing), &allowMixing);

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

NSLog(@"Mixing: %x", propertySetError); // This should be 0 or there was an issue somewhere

[[AVAudioSession sharedInstance] setActive:YES error:nil];

Hope that helps someone!

2
votes

I answered it here already: How to get AVAudioPlayer output to the speaker

In short, use this before recording:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];

...and use this before playback (on loud speakers or headphones if they are plugged in)

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
1
votes

Only thing I have found about this topic is this:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

which must be set when you record your audio if you want to play back at the same time. Give that a try and lemme know.

P.S. Make sure you add the AudioToolbox and AVFoundation frameworks to your project and include them in your .m files.

0
votes

If you're currently playing through the quiet speakers, and want to play through the loud speaker at the bottom of the iPhone, use this code:

UInt32 doChangeDefaultRoute = 1;

AudioSessionSetProperty (
                         kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
                         sizeof (doChangeDefaultRoute),
                         &doChangeDefaultRoute
                         );
0
votes

This is old question, but none of the other answers helped me... However, I found a solution which I am posting for future reference in case someone needs it.

The solution is described in the following blog post: iOS: Force audio output to speakers while headphones are plugged in .

You need to create new Objective-C class AudioRouter in your project. Then import AudioRouter.h into your header file of the class where you are initiating audio functionality. Now in the corrseponding .m file add the following lines within viewDidLoad method:

AudioRouter *foobar = [[AudioRouter alloc] init];
[foobar initAudioSessionRouting];
[foobar forceOutputToBuiltInSpeakers];

Now you have audio (e.g. AVAudioPlayer) output forced to loudspeaker!

0
votes

AudioSessionSetProperty is deprecated since iOS7 but the following worked for me.

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];