Detecting if headphones are plugged into iPhone

ingh.am picture ingh.am · Aug 26, 2010 · Viewed 11.9k times · Source

Does anyone know if you can detect if headphones are plugged into the iPhone, and if they aren't - disable sound from your application.

I think I could manage disabling sound, but the detection part I have yet to find anything on.

Thanks

Answer

xlarsx picture xlarsx · Jun 25, 2012

With this code you can detect the changes between:

  • MicrophoneWired
  • Headphone
  • LineOut
  • Speaker

Detecting when an iOS Device connector was plugged/unplugged

Note: Since iOS 5 part of the "audioRouteChangeListenerCallback(...)" behavior is deprecated but you can update it with:

// kAudioSession_AudioRouteChangeKey_PreviousRouteDescription -> Previous route
// kAudioSession_AudioRouteChangeKey_CurrentRouteDescription -> Current route

CFDictionaryRef newRouteRef = CFDictionaryGetValue(routeChangeDictionary, kAudioSession_AudioRouteChangeKey_CurrentRouteDescription);
NSDictionary *newRouteDict = (NSDictionary *)newRouteRef;

// RouteDetailedDescription_Outputs -> Output
// RouteDetailedDescription_Outputs -> Input

NSArray * paths = [[newRouteDict objectForKey: @"RouteDetailedDescription_Outputs"] count] ? [newRouteDict objectForKey: @"RouteDetailedDescription_Outputs"] : [newRouteDict objectForKey: @"RouteDetailedDescription_Inputs"];

NSString * newRouteString = [[paths objectAtIndex: 0] objectForKey: @"RouteDetailedDescription_PortType"];

// newRouteString -> MicrophoneWired, Speaker, LineOut, Headphone

Greetings