Last active
August 27, 2019 09:17
-
-
Save rpechayr/6681746 to your computer and use it in GitHub Desktop.
Watch source change (Headphone vs. Internal Speaker)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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