1

I made my app can select audio-output. (like 'system default' or 'user's DAC')

but when user choose a output from system preferences panel - sound, my app's output follows the output user seleced.

I searched a lot and add some listener so I can change immediatly my app's output to previously user selected if system output has been changed.

BUT it makes very anonying few milliseconds swiching delay.

I guess it is because I switch my app's output after it's already changed to system default.

So I wonder If I can know BEFORE system default output's changing. (Like viewWillAppear api from cocoa)

Thank you.

listener that I used for knowing chaninging of system default audio out is from the article below.

How to get notification if System Preferences Default Sound changed

thanks

more details

I used AudioUnitSetProperty(audioOut, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Output, 0, &deviceID, (UInt32)sizeof(deviceID)) for selecting output device. apple document

and add this listener

func addListenerBlock(listenerBlock: @escaping AudioObjectPropertyListenerBlock, onAudioObjectID: AudioObjectID, forPropertyAddress: inout AudioObjectPropertyAddress) {
                if (kAudioHardwareNoError != AudioObjectAddPropertyListenerBlock(onAudioObjectID, &forPropertyAddress, nil, listenerBlock)) {
                    LOG("Error calling: AudioObjectAddPropertyListenerBlock") }
            }

func add() {

        var propertyAddress = AudioObjectPropertyAddress(mSelector: kAudioHardwarePropertyDefaultOutputDevice,
                                                         mScope: kAudioObjectPropertyScopeGlobal,
                                                         mElement: kAudioObjectPropertyElementMaster)
        self.addListenerBlock(listenerBlock: audioObjectPropertyListenerBlock,
                                          onAudioObjectID: AudioObjectID(bitPattern: kAudioObjectSystemObject),
                                          forPropertyAddress: &propertyAddress)
    }
WoffOVkee
  • 435
  • 3
  • 16
  • You need to explain what APIs you're using. How are you setting things up? How are you switching the device? Etc. What exactly is happening and how you will need to change things depends on what you're currently doing. – Ken Thomases Sep 11 '17 at 02:42
  • I get device ID and call `AudioUnitSetProperty(audioOut, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Output, 0, &deviceID, (UInt32)sizeof(deviceID))` for selecting output device. [referene](https://developer.apple.com/documentation/audiotoolbox/1440371-audiounitsetproperty?language=objc) – WoffOVkee Sep 11 '17 at 02:47
  • How/from where do you get the device ID? – Ken Thomases Sep 11 '17 at 04:13
  • key api that i'm using for getting device ids is `AudioObjectGetPropertyData(AudioObjectID(kAudioObjectSystemObject), &devicesPropertyAddress, 0, nil, &propertySize, &deviceIDs)` and whole codes you can look from [HERE](https://stackoverflow.com/questions/4575408/audioobjectgetpropertydata-to-get-a-list-of-input-devices) (find 'func getInputDevices()'. (whole codes is too long to write here :) ) – WoffOVkee Sep 11 '17 at 04:21

1 Answers1

1

kAudioUnitSubType_DefaultOutput tracks the current output device selected by the user in the Sound Preferences. To play to a specific device use kAudioUnitSubType_HALOutput. The comments in AUComponent.h are helpful:

@enum           Apple input/output audio unit sub types (OS X)
@constant       kAudioUnitSubType_HALOutput         
                    - desktop only
                The audio unit that interfaces to any audio device. The user specifies which 
                audio device to track. The audio unit can do input from the device as well as 
                output to the device. Bus 0 is used for the output side, bus 1 is used
                to get audio input from the device.

@constant       kAudioUnitSubType_DefaultOutput     
                    - desktop only
                A specialisation of AUHAL that is used to track the user's selection of the 
                default device as set in the Sound Prefs

@constant       kAudioUnitSubType_SystemOutput      
                    - desktop only
                A specialisation of AUHAL that is used to track the user's selection of the 
                device to use for sound effects, alerts
                and other UI sounds.

You didn't specify how you're setting up your output (AUGraph?) so the way to use kAudioUnitSubType_HALOutput varies.

sbooth
  • 16,646
  • 2
  • 55
  • 81