I've written an app that plays audio using AVAudioPlayer.
I've implemented a function that switches the audio to the speaker:
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
But when I try to switch it back to the Headphones using this:
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
it remains routed through the speaker.
I cannot find any other way of directing the audio. Can anyone suggest what I'm doing wrong?
According to the Apple's documentation, the Audio Session route override is only availble when using the kAudioSessionCategory_PlayAndRecord
category.
You can try changing the category to kAudioSessionCategory_PlayAndRecord
and use the kAudioSessionProperty_OverrideCategoryDefaultToSpeaker
property key to route audio to the speaker.