1

I am using SpeakHere example code provided by Apple in my project.

How can I enable speakers If earphones or headphones are not plugged in?

I know I can overwrite the route using the following code..

OSStatus error;
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; 
error = AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute, sizeof (audioRouteOverride), &audioRouteOverride);
if (error) printf("couldn't set audio speaker!");

But I don't want that because if earphones are plugged in, the sound should be coming out of them..

Tim Tuffley
  • 605
  • 1
  • 6
  • 20
  • Look at the upvote-17 answer at http://stackoverflow.com/questions/3728781/detect-if-headphones-not-microphone-are-plugged-in-to-an-ios-device –  Jul 16 '13 at 15:11

2 Answers2

1

here is a headphone/accessory detect method stolen/borrowed from here Detect if headphones (not microphone) are plugged in to an iOS device

using this and a if statement with your method below will get you your results.

 - (BOOL)isHeadsetPluggedIn {
    UInt32 routeSize = sizeof (CFStringRef);
    CFStringRef route;

    OSStatus error = AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
                                              &routeSize,
                                              &route);

    /* Known values of route:
     * "Headset"
     * "Headphone"
     * "Speaker"
     * "SpeakerAndMicrophone"
     * "HeadphonesAndMicrophone"
     * "HeadsetInOut"
     * "ReceiverAndMicrophone"
     * "Lineout"
     */

    if (!error && (route != NULL)) {

        NSString* routeStr = (NSString*)route;

        NSRange headphoneRange = [routeStr rangeOfString : @"Head"];

        if (headphoneRange.location != NSNotFound) return YES;

    }

    return NO;
}
Community
  • 1
  • 1
rezand
  • 576
  • 3
  • 11
1

Most likely this is what you are looking for:

    UInt32 overrideValue = 1;
    AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(overrideValue), &overrideValue);

From the docs: 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.

Lewis Gordon
  • 1,711
  • 14
  • 19
  • yeah.. worked pretty well.. but this time, even if the device is on silent, sound keeps coming out of speakers.. – Tim Tuffley Jul 17 '13 at 04:45
  • Never noticed that before, but Apple apps do the same - the music player and voice memo both continue to play on silent mode. – Lewis Gordon Jul 17 '13 at 05:13
  • Thanks for your answer.. Can you please check my other question regarding SpeakHere example.. http://stackoverflow.com/questions/17691616/speakhere-voice-recorder-fails-after-avaudioplayer-in-ios – Tim Tuffley Jul 17 '13 at 05:16
  • The silent mode operation is actually controlled by the audio session category. Playback and play and record override silent mode since the primary function of the app would be to play audio. See docs for more info. – Lewis Gordon Jul 17 '13 at 06:20