tags:

views:

156

answers:

2

Hi,

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

A: 

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.

iWasRobbed
Yes, this is needed, but i got it. No change :(The function itself is okay, you can blow the mic and hear the sound, but only with the internal speaker.
Max Steinmeyer
+1  A: 

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!

Cory Imdieke