From 715dc6d19909a107b378d35cd6b6f8d30a6a9378 Mon Sep 17 00:00:00 2001 From: Alex Man Date: Sat, 26 Dec 2020 22:58:50 -0800 Subject: [PATCH] Fix coreaudio module not detecting changes of the default audio device. --- src/coreaudio.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/coreaudio.c b/src/coreaudio.c index 5cee4fc..00c6d2d 100644 --- a/src/coreaudio.c +++ b/src/coreaudio.c @@ -145,6 +145,12 @@ static void destroy_ca(struct SoundIoPrivate *si) { }; AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &prop_address, on_devices_changed, si); + prop_address.mSelector = kAudioHardwarePropertyDefaultOutputDevice; + AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &prop_address, on_devices_changed, si); + + prop_address.mSelector = kAudioHardwarePropertyDefaultInputDevice; + AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &prop_address, on_devices_changed, si); + prop_address.mSelector = kAudioHardwarePropertyServiceRestarted; AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &prop_address, on_service_restarted, si); @@ -1446,6 +1452,22 @@ int soundio_coreaudio_init(struct SoundIoPrivate *si) { return SoundIoErrorSystemResources; } + prop_address.mSelector = kAudioHardwarePropertyDefaultOutputDevice; + if ((err = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &prop_address, + on_devices_changed, si))) + { + destroy_ca(si); + return SoundIoErrorSystemResources; + } + + prop_address.mSelector = kAudioHardwarePropertyDefaultInputDevice; + if ((err = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &prop_address, + on_devices_changed, si))) + { + destroy_ca(si); + return SoundIoErrorSystemResources; + } + prop_address.mSelector = kAudioHardwarePropertyServiceRestarted; if ((err = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &prop_address, on_service_restarted, si)))