Does anyone know if it is possible to implement playback of an audio file through the internal speaker even if the headphones are plugged in?
3 Answers
I'm not sure how you are doing your audio playback, but try having a look at the "Redirecting Output Audio" section of the Audio Session Programming Guide
It looks something like this:
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; // 1
AudioSessionSetProperty (
kAudioSessionProperty_OverrideAudioRoute, // 2
sizeof (audioRouteOverride), // 3
&audioRouteOverride // 4
);

- 22,595
- 11
- 77
- 101
-
1Does this change the route for the input as well? I want to take input from external mic and want to use the internal speakers for the output. Can I do that using this method? – Namratha Aug 10 '11 at 09:46
-
I'm not sure, when you say external mic, do you mean the device microphone instead of a headset microphone? – slf Aug 11 '11 at 14:32
-
2No no I meant the mic that is externally plugged in. The input should be taken from that. I got my issue resolved. Found your post very helpful! – Namratha Aug 12 '11 at 06:43
-
@slf i want to record sound that is played in background using AudioPlayer,at same time i want to disable External noise(External mic). is this Possible??? – Apple Mar 27 '12 at 04:44
-
@Aelish from what it sounds like, you need to watch use audio queue services and monitor it for information. You don't actually want to 'record' anything at all so the microphone is irrelevant. I don't know if the audio data is available if you don't have a handle to the queue, in other words, I don't know if you can record audio from say, music playing in iTunes or Pandora on the device – slf Mar 28 '12 at 13:54
-
@Namratha Have you found how to have audio route to speaker but record from headset microphone ? – oqu Jul 05 '12 at 19:14
-
I have seen this solution several times but I just do not know where to place that piece of code. Can Someone please tell me? – meda Apr 30 '13 at 22:48
-
1I want to do the opposite: when the Apple-supplied headphones (that have a built-in mic) are plugged into the iPhone I want to use the device's built-in mic (not the mic on the headphone) while listening to audio through the headphones (not the receiver or speaker built-in to the device). Any ideas on how to do this? It seems like it would be similar to your solution. – Nick May 26 '13 at 23:08
-
@Nick For a route change event, the system sends the [kAudioSessionProperty_AudioRouteChange](https://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/AudioSessionServicesReference/Reference/reference.html#//apple_ref/c/econst/kAudioSessionProperty_AudioRouteChange) in the inID parameter – slf May 27 '13 at 14:47
-
@Nick route change apple example: [AddMusic](https://developer.apple.com/library/ios/#samplecode/AddMusic/Listings/Classes_MainViewController_m.html#//apple_ref/doc/uid/DTS40008845-Classes_MainViewController_m-DontLinkElementID_6) – slf May 27 '13 at 14:49
Actually i think this is not possible, as there seems to be some kind of mechanical switch, which indicates a plugged in headset thus preventing speaker output when this is the case. (read here)
Some other hints can be found in the description of kAudioSessionProperty_OverrideCategoryDefaultToSpeaker
:
"Specifies whether or not to route audio to the speaker (instead of to the receiver) when no other audio route, such as a headset, is connected."

- 846
- 11
- 31
Here's a solution for anyone arriving here since the AudioSessionSetProperty
(and related APIs) have been deprecated in iOS 7.0.
- (BOOL)setupSpeakerRouteOverride {
NSError *avAudioErr = nil;
BOOL result = [[AVAudioSession sharedInstance]
overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker
error:&avAudioErr];
if (avAudioErr) {
// Handle Error
NSLog(@"Error setting speaker route override: %@",[avAudioErr localizedDescription]);
}
return result;
}
Notes:
- This is part of the
AVAudioSession
API and the relevant Apple documentation is here. - Important notes from the API discussion section:
If your app uses the AVAudioSessionCategoryPlayAndRecord category, calling this method with the AVAudioSessionPortOverrideSpeaker option causes the system to route audio to the built-in speaker and microphone regardless of other settings. This change remains in effect only until the current route changes or you call this method again with the AVAudioSessionPortOverrideNone option.
If you’d prefer to permanently enable this behavior, you should instead set the category’s AVAudioSessionCategoryOptionDefaultToSpeaker option. Setting this option routes to the speaker rather than the receiver if no other accessory such as headphones are in use.

- 621
- 8
- 13