Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting if headphones are plugged into iPhone

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

like image 629
ingh.am Avatar asked Aug 26 '10 13:08

ingh.am


People also ask

How can my phone detect my headphones?

By default, it needs to see 8-700 Ω on one of the headphones pins to detect a device, and looks for 1.5 to 20 kΩ between R2 and S to detect a headset. It can also check whether it's OMTP or CTIA headset wiring.

Why won't my iPhone detect my headphones?

Check for debris in the headphone port on your iPhone, iPad or iPod touch. Check your headphone cable, connector, remote, and earbuds for damage, like wear or breakage. Look for debris on the meshes in each earbud. To remove debris, gently brush all openings with a small, soft-bristled brush that's clean and dry.


2 Answers

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

like image 75
xlarsx Avatar answered Oct 02 '22 05:10

xlarsx


http://developer.apple.com/iphone/library/samplecode/SpeakHere/Introduction/Intro.html

In this project there is a code-snippet where it pauses recording if the headphones is unpluged. Maybe you can use it to achieve your result.

Good luck!

(edit)

You will have to study the SpeakHereController.mm file.
I found this code in the awakeFromNib method

// we do not want to allow recording if input is not available
error = AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &size, &inputAvailable);
if (error) printf("ERROR GETTING INPUT AVAILABILITY! %d\n", error);
btn_record.enabled = (inputAvailable) ? YES : NO;

// we also need to listen to see if input availability changes
error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioInputAvailable, propListener, self);
if (error) printf("ERROR ADDING AUDIO SESSION PROP LISTENER! %d\n", error);
like image 25
hellozimi Avatar answered Oct 02 '22 07:10

hellozimi