{"id":127,"date":"2008-09-11T15:34:57","date_gmt":"2008-09-11T20:34:57","guid":{"rendered":"http:\/\/vgable.com\/blog\/2008\/09\/11\/detecting-if-headphones-are-plugged-in\/"},"modified":"2008-11-10T16:35:10","modified_gmt":"2008-11-10T21:35:10","slug":"detecting-if-headphones-are-plugged-in","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2008\/09\/11\/detecting-if-headphones-are-plugged-in\/","title":{"rendered":"Detecting if Headphones are Plugged In"},"content":{"rendered":"<p>I found many posts on mailing lists asking the question of how to detect if something is plugged into the headphones jack, but no complete answer.  So here&#8217;s how I do it.<\/p>\n<p><strong>Docs &#038; Tools<\/strong><\/p>\n<p>Use the <a href=\"http:\/\/developer.apple.com\/documentation\/MusicAudio\/Conceptual\/CoreAudioOverview\/CoreAudioFrameworks\/chapter_950_section_5.html\">CoreAudio.framework<\/a>, specifically <a href=\"http:\/\/developer.apple.com\/documentation\/MusicAudio\/Reference\/CACoreAudioReference\/AudioHardware\/CompositePage.html\"><code>CoreAudio\/CoreAudio.h<\/code><\/a>  Unfortunately <a href=\"http:\/\/www.mikeash.com\/?page=pyblog\/why-coreaudio-is-hard.html\">it&#8217;s a bit confusing sometimes<\/a>, but the documentation is still very complete (if complex).<\/p>\n<p>HALLab (<code>\/Developer\/Examples\/CoreAudio\/HAL\/HALLab\/<\/code> if you have the developer tools installed) is the most complex bit of relevant CoreAudio sample code, and when built it&#8217;s a useful utility for poking-around with to boot.<\/p>\n<p><strong>How To Do It<\/strong><\/p>\n<p>Whenever something is plugged into the headphones jack, CoreAudio changes the data source for the output device, from internal speakers to headphones.  Use <code>AudioDeviceGetProperty(outputDeviceID, 0, 0, kAudioDevicePropertyDataSource, &amp;size, &amp;dataSource);<\/code> to examine the 32-bit ID of the data source, which is best thought of as a <a href=\"http:\/\/vgable.com\/blog\/2008\/04\/23\/printing-a-fourcharcode\/\">4 character code<\/a>.  If <code>dataSource<\/code> is <code>'ispk'<\/code>, sound is played through internal speakers, if it is <code>'hdpn'<\/code>, sound is playing through the headphones jack.  I&#8217;m not sure what happens if you have speakers plugged in that use a different output source, like USB, FireWire, or optical out.<\/p>\n<p>A word of caution, do not use the promising-looking AudioDevice Property <code>kAudioDevicePropertyJackIsConnected<\/code> it does not work on all systems (specifically it didn&#8217;t work in 10.4, and I have not tested it under Leopard).<\/p>\n<p><strong>Change Notifications<\/strong><\/p>\n<p>If you want to be notified when something is plugged\/unplugged in the headphones jack, listen for a <code>kAudioDevicePropertyDataSource<\/code> change to the output audio device.  Here is the code I use to test if a computer is using it&#8217;s internal speakers:<\/p>\n<p><strong>Sample Code:<\/strong><\/p>\n<p><code><br \/>\n\/\/Returns YES if the default sound output device<br \/>\/\/is using external speakers to play sound.<br \/>- (BOOL) usingInternalSpeakers<br \/>{<br \/>&nbsp;&nbsp;&nbsp;AudioDeviceID deviceID;<br \/>&nbsp;&nbsp;&nbsp;UInt32 size = sizeof(deviceID);<br \/>&nbsp;&nbsp;&nbsp;OSStatus err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultSystemOutputDevice, &amp;size, &amp;deviceID);<br \/>&nbsp;&nbsp;&nbsp;NSCAssert((err == noErr), @\"AudioHardwareGetProperty failed to get the kAudioHardwarePropertyDefaultSystemOutputDevice property\");<br \/>&nbsp;&nbsp;&nbsp;\/\/To be notified when something is plugged\/unplugged into the headphones jack<br \/>&nbsp;&nbsp;&nbsp;\/\/listen for a kAudioDevicePropertyDataSource or kAudioDevicePropertyDataSources notification on deviceID<br \/>&nbsp;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;\/\/Check if headphones are plugged in right now:<br \/>&nbsp;&nbsp;&nbsp;UInt32 dataSource;<br \/>&nbsp;&nbsp;&nbsp;size = sizeof(dataSource);<br \/>&nbsp;&nbsp;&nbsp;err = AudioDeviceGetProperty(deviceID, 0, 0, kAudioDevicePropertyDataSource, &amp;size, &amp;dataSource);<br \/>&nbsp;&nbsp;&nbsp;NSCAssert((err == noErr), @\"AudioDeviceGetProperty failed to get the kAudioDevicePropertyDataSource property\");<br \/>&nbsp;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;\/\/'ispk' =&gt; internal speakers<br \/>&nbsp;&nbsp;&nbsp;\/\/'hdpn' =&gt; headphones<br \/>&nbsp;&nbsp;&nbsp;return dataSource == 'ispk';<br \/>}<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I found many posts on mailing lists asking the question of how to detect if something is plugged into the headphones jack, but no complete answer. So here&#8217;s how I do it. Docs &#038; Tools Use the CoreAudio.framework, specifically CoreAudio\/CoreAudio.h Unfortunately it&#8217;s a bit confusing sometimes, but the documentation is still very complete (if complex). [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4,13],"tags":[223,172,79],"class_list":["post-127","post","type-post","status-publish","format-standard","hentry","category-macosx","category-programming","category-sample-code","tag-audiohardwaregetproperty","tag-coreaudio","tag-fourcharcode"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/comments?post=127"}],"version-history":[{"count":0,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/127\/revisions"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}