Cruinh
Cruinh

Reputation: 3721

iPhone audio playback: force through internal speaker?

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

Answers (3)

Mike C.
Mike C.

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:

  1. This is part of the AVAudioSession API and the relevant Apple documentation is here.
  2. 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.

  1. If the temporary nature of the override is appropriate for your application, you could react to audio route changes by subscribing to the AVAudioSessionRouteChangeNotification notification and handle the events accordingly. (See here.)
  2. For a similar solution is Swift, see this question's answer.

Upvotes: 0

Maximilian Körner
Maximilian Körner

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

slf
slf

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

Related Questions