1

Our app uses the remoteIO (audio-unit) to get audio input from mic, and DSP over it in real time, it has a callback function that gives me the buffer, and I work on it.

At the same time, we need to play sounds over avAudioPlayer.

I put in my init this : (route the sounds to speaker and let it play and record)

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

but then I have seen something strange. The sounds are playing from the small speaker - the one you use to put your ears on, in phone calls.

Then I did a trick. I have seen that in order to really route the sounds to the speakers, I have to play something and in the same time execute this 4 lines above. If I do it 1 time, then the sounds are moving back to the speakers forever. I did that with a timer that calls the method, and the moment this method executes them, I play sound with avaudioPlayer, then-it fix it and sound routes to speakers.

Any one has any solution, or explanation about that thing? I really need to play sounds via speakers, without tricks.

Waynn Lue
  • 11,344
  • 8
  • 51
  • 76
user1280535
  • 347
  • 3
  • 13

1 Answers1

3

The reason why it plays forever is that the mic picks up the sound played and it enters a loop AKA the Larsen effect

You can force the sound to the bottom speaker with

-(void)setSpeaker
{
    OSStatus error;
    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; 
    error = AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute, sizeof (audioRouteOverride), &audioRouteOverride);
    if (error) NSLog(@"Couldn't route audio to speaker");
}

You can also present a UI to the user to chose the bottom or top speaker with a MPVolumeView

valexa
  • 4,462
  • 32
  • 48