如何忽略系统偏好设置中更改的音频输出? (苹果系统)

How to ignore changing audio-output from System Preference? (macOS)

我让我的应用程序可以 select 音频输出。 (如 'system default' 或“用户的 DAC”)

但是当用户从系统首选项面板选择输出 - 声音时,我的应用程序的输出会遵循用户选择的输出。

我进行了大量搜索并添加了一些侦听器,因此如果系统输出已更改,我可以立即将应用程序的输出更改为以前的用户 selected。

但是它会造成非常烦人的几毫秒切换延迟。

我想这是因为我在应用程序的输出已经更改为系统默认值后切换了它。

所以我想知道我是否可以在系统默认输出更改之前知道。 (就像 cocoa 中的 viewWillAppear api)

谢谢。

我用来了解系统默认音频输出的监听器来自下面的文章。

How to get notification if System Preferences Default Sound changed

谢谢

更多详情

我使用 AudioUnitSetProperty(audioOut, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Output, 0, &deviceID, (UInt32)sizeof(deviceID)) 作为 selecting 输出设备。 apple document

并添加这个监听器

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)
    }

kAudioUnitSubType_DefaultOutput 跟踪用户在声音首选项中选择的当前输出设备。要在特定设备上播放,请使用 kAudioUnitSubType_HALOutputAUComponent.h 中的评论很有帮助:

@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.

您没有指定如何设置输出(AUGraph?)所以使用 kAudioUnitSubType_HALOutput 的方式不同。