This is fairly simple (I think). I'm simply wanting to get a notification in my application whenever a user changes the default sound input or sound output device in System Preferences - Sound. I'll be darned if I'm able to dig it out of the Apple docs, however.
As a side note, this is for OSX, not IOS.
Thanks guys!
Set up an
AudioObjectPropertyAddress
for the default output device:Then, use
AudioObjectSetPropertyData
to register a listener for changes in the default device:The callback function looks like this:
As a side note, you should also use
AudioObjectPropertyAddress
to tell the HAL to manage its own thread for notifications. You do this by setting the run loop selector to NULL. I actually perform this step before setting up the output device listener.Here's how to do it in Swift:
Register for notifications by adding a listener block, for example when a View Controller loads its view. The
addListenerBlock
function simplifies adding property listener blocks. Swift allows parameters to be variables, which is very convenient for theforPropertyAddress
parameter. When callingaddListenerBlock
, the property address parameters are just plugged in.Provide a listener block function to receive the notifications. You're given an array that could potentially have more than one property address, so loop through and look for the one you want:
Of course, you can simply add more
cases
to theswitch
statement if you want to monitor other audio device properties. CalladdListenerBlock
to add whatever devices and property addresses you're interested in.