Reputation: 3721
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?
Upvotes: 9
Views: 7132
Reputation: 771
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:
AVAudioSession
API and the relevant Apple documentation is here.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.
AVAudioSessionRouteChangeNotification
notification and handle the events accordingly. (See here.)Upvotes: 0
Reputation: 894
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."
Upvotes: 0
Reputation: 22767
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
);
Upvotes: 10