1

I'm working on iPad. I would like to detect when user plug-out headphone. First I used a listener on the property kAudioSessionProperty_AudioRouteChange. So all was working well until I've decided to add a button to switch to speakers when headphone was still plugged. So I’m now facing a problem, maybe someone would have an idea to fix it.

Here is the scenario :

  • I plug a headphone -> my audio route change callback is called
  • then I switch sound to speakers (without unplugging my headphone) -> audio route change callback is called
  • then I unplug my headphone (when sound is still outputting to speakers) -> audio route change callback is NOT called, which seems logical.

But here is my problem ! So my question is : Do you see a way to detect that headphone was unplugged for this last case ?

Thanks for your help

EDIT :

Ok I found a workaround :

To detect whether or not headphones are plugged, I execute a test function all the times I need to know it (instead using a boolean), this might be less good for performances but it's working, here is my code for the ones who may need it :

//set back the default audio route
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);

//check if this default audio route is Heaphone or Speaker
CFStringRef newAudioRoute;
UInt32 newAudioRouteSize = sizeof(newAudioRoute);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &newAudioRouteSize, &newAudioRoute);

NSString *newAudioRouteString = (__bridge NSString *)newAudioRoute;

CFRelease(newAudioRoute);

//if this default audio route is not Headphone, it means no headphone is plugged
if ([newAudioRouteString rangeOfString:@"Headphones"].location != NSNotFound){
    NSLog(@"Earphone available");
    return true;
}
else {
    NSLog(@"No Earphone available");
    return false;
}

Hope it will help someone !

Cyril
  • 31
  • 1
  • 6
  • Did you try to switch the sound to the headphone back, and then try to unplug your headphones? I think, the iPad is switched to speakers, and when you unplug the headphone, it thinks, it isn't necessary to call again the function, it is already set on the speakers. – MMMM May 03 '12 at 08:00
  • Yes I agree with you, but my need is to be able to detect that a user unplugged the headphone, even when speakers are used. I need this to set a boolean to false. Because user always does what he wants, so I have to handle all cases... – Cyril May 03 '12 at 08:48
  • In the second case, when you switch the speakers on, even with the headphone, you have to be able to hear the sound on the speakers and the headphone too, or only on the speakers? – MMMM May 03 '12 at 09:02

2 Answers2

0

I imagine a solution it in the following way: You create in the AppDelegate a boolean for the speakers, let's say: BOOL isSpeakerOn. And every time the audio route callback is called you have to verify what the current situation with the speakers and what you want to do then.

MMMM
  • 1,319
  • 1
  • 14
  • 32
  • I would rather need a boolean like `BOOL isHeadphonePlugged`. My problem is that the callback is not called if the route doesn't change. So I would like to know if there is another way to detect the event "headphone plug-out" – Cyril May 03 '12 at 09:21
0

This is the best tutorial dealing this issue:

http://www.techotopia.com/index.php/Detecting_when_an_iPhone_Headphone_or_Docking_Connector_is_Unplugged_(iOS_4)

Yossi
  • 2,525
  • 2
  • 21
  • 24