Skip to content

Instantly share code, notes, and snippets.

@rpechayr
Last active August 27, 2019 09:17
Show Gist options
  • Save rpechayr/6681746 to your computer and use it in GitHub Desktop.
Save rpechayr/6681746 to your computer and use it in GitHub Desktop.
Watch source change (Headphone vs. Internal Speaker)
NSLog(@"Starting");
AudioDeviceID defaultDevice = 0;
UInt32 defaultSize = sizeof(AudioDeviceID);
const AudioObjectPropertyAddress defaultAddr = {
kAudioHardwarePropertyDefaultOutputDevice,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
AudioObjectGetPropertyData(kAudioObjectSystemObject, &defaultAddr, 0, NULL, &defaultSize, &defaultDevice);
AudioObjectPropertyAddress sourceAddr;
sourceAddr.mSelector = kAudioDevicePropertyDataSource;
sourceAddr.mScope = kAudioDevicePropertyScopeOutput;
sourceAddr.mElement = kAudioObjectPropertyElementMaster;
AudioObjectAddPropertyListenerBlock(defaultDevice, &sourceAddr, dispatch_get_current_queue(), ^(UInt32 inNumberAddresses, const AudioObjectPropertyAddress *inAddresses) {
UInt32 dataSourceId = 0;
UInt32 dataSourceIdSize = sizeof(UInt32);
AudioObjectGetPropertyData(defaultDevice, &sourceAddr, 0, NULL, &dataSourceIdSize, &dataSourceId);
NSLog(@"Input changed");
if (dataSourceId == 'ispk') {
NSLog(@"Internal Speaker");
} else if (dataSourceId == 'hdpn') {
NSLog(@"Headphone");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment