<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Vincent Gable's Blog &#187; AudioHardwareGetProperty</title>
	<atom:link href="http://vgable.com/blog/tag/audiohardwaregetproperty/feed/" rel="self" type="application/rss+xml" />
	<link>http://vgable.com/blog</link>
	<description>my weblog.</description>
	<lastBuildDate>Tue, 29 Nov 2011 22:20:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Detecting if Headphones are Plugged In</title>
		<link>http://vgable.com/blog/2008/09/11/detecting-if-headphones-are-plugged-in/</link>
		<comments>http://vgable.com/blog/2008/09/11/detecting-if-headphones-are-plugged-in/#comments</comments>
		<pubDate>Thu, 11 Sep 2008 20:34:57 +0000</pubDate>
		<dc:creator>Vincent Gable</dc:creator>
				<category><![CDATA[MacOSX]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Sample Code]]></category>
		<category><![CDATA[AudioHardwareGetProperty]]></category>
		<category><![CDATA[CoreAudio]]></category>
		<category><![CDATA[FourCharCode]]></category>

		<guid isPermaLink="false">http://vgable.com/blog/2008/09/11/detecting-if-headphones-are-plugged-in/</guid>
		<description><![CDATA[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). [...]]]></description>
			<content:encoded><![CDATA[<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>
<p><strong>Docs &#038; Tools</strong></p>
<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>
<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>
<p><strong>How To Do It</strong></p>
<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>
<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>
<p><strong>Change Notifications</strong></p>
<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>
<p><strong>Sample Code:</strong></p>
<p><code><br />
//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 />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://vgable.com/blog/2008/09/11/detecting-if-headphones-are-plugged-in/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

