2017-09-11 59 views
0

我让我的应用程序可以选择音频输出。 (如“系统默认”或“用户的DAC”)如何忽略系统偏好改变的音频输出? (macOS)

但是当用户选择从系统首选项面板中的输出 - 声音,我的应用程序的输出如下seleced输出用户。

我搜索了很多,并添加一些侦听器,以便我可以在我的应用程序的输出立刻改变先前用户选择,如果系统输出已经改变。

但它使非常anounying几毫秒swiching延迟。

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

所以我想知道如果我可以知道系统默认输出的变化。 (可可像viewWillAppear中API)

谢谢。我用于得知系统默认的音频chaninging出

监听器是从下面的文章。

How to get notification if System Preferences Default Sound changed

感谢

详情

我用AudioUnitSetProperty(audioOut, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Output, 0, &deviceID, (UInt32)sizeof(deviceID))用于选择输出设备。 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) 
    } 
+0

您需要解释您正在使用的API。你怎么设置的?你如何切换设备?等等到底发生了什么以及您需要如何改变事情取决于您目前的工作。 –

+0

我得到设备ID和呼叫'AudioUnitSetProperty(AUDIOOUT,kAudioOutputUnitProperty_CurrentDevice,kAudioUnitScope_Output,0,&设备ID,(UInt32的)的sizeof(设备ID))'用于选择输出设备。 [参考](https://developer.apple.com/documentation/audiotoolbox/1440371-audiounitsetproperty?language=objc) – okskpark

+0

如何/从哪里获取设备ID?我使用了获取设备ID –

回答

1

kAudioUnitSubType_DefaultOutput跟踪由在声音首用户选择的电流输出装置。要玩特定设备,请使用kAudioUnitSubType_HALOutput。在AUComponent.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变化的方式。

+0

这正是我所知试图找到!非常感谢你的XD – okskpark