2014-09-27 62 views
2

這很簡單(我認爲)。我只是想在用戶更改系統偏好設置 - 聲音中的默認聲音輸入或聲音輸出設備時,在我的應用程序中獲得通知。但是,如果我能夠從Apple文檔中挖掘出來,我將會感到厭煩。如何獲得系統偏好設置默認聲音變化

作爲一個方面說明,這是用於OSX,而​​不是IOS。

謝謝你們!

+0

嘗試使用搜索(http://stackoverflow.com/questions/6747016/how-do-i-register-for-a-notification-for - 然後音量改變) – toohtik 2014-09-29 14:57:18

+0

當默認聲音輸入或輸出設備選擇改變時,這不起作用。非常適合檢測音量級別,不適用於檢測設備選擇更改。 – 2014-09-30 14:46:43

回答

2

搭建AudioObjectPropertyAddress爲默認輸出設備:

AudioObjectPropertyAddress outputDeviceAddress = { 
    kAudioHardwarePropertyDefaultOutputDevice, 
    kAudioObjectPropertyScopeGlobal, 
    kAudioObjectPropertyElementMaster 
}; 

然後,使用AudioObjectSetPropertyData註冊一個監聽器在默認設備更改:

AudioObjectAddPropertyListener(kAudioObjectSystemObject, 
           &outputDeviceAddress, 
           &callbackFunction, nil); 

回調函數如下:

OSStatus callbackFunction(AudioObjectID inObjectID, 
          UInt32 inNumberAddresses, 
          const AudioObjectPropertyAddress inAddresses[],    
          void *inClientData) 

作爲一個附註,你也應該使用AudioObjectPropertyAddress來通知HAL管理自己的線程以進行通知。您可以通過將運行循環選擇器設置爲NULL來完成此操作。實際上,在設置輸出設備偵聽器之前執行此步驟。

AudioObjectPropertyAddress runLoopAddress = { 
    kAudioHardwarePropertyRunLoop, 
    kAudioObjectPropertyScopeGlobal, 
    kAudioObjectPropertyElementMaster 
}; 

CFRunLoopRef runLoop = NULL; 
UInt32 size = sizeof(CFRunLoopRef); 
AudioObjectSetPropertyData(kAudioObjectSystemObject, 
          &runLoopAddress, 0, NULL, size, &runLoop); 
2

這裏是如何做到這一點的斯威夫特:

  1. 註冊時通過視圖控制器加載其視圖添加監聽塊,例如通知。 addListenerBlock函數簡化了添加屬性偵聽器塊。 Swift允許參數爲變量,這對於forPropertyAddress參數非常方便。當調用addListenerBlock,物業地址參數只是插入。

    import Cocoa 
    import CoreAudio 
    
    class ViewController: NSViewController { 
    
        // Utility function to simplify adding listener blocks: 
        func addListenerBlock(listenerBlock: AudioObjectPropertyListenerBlock, onAudioObjectID: AudioObjectID, var forPropertyAddress: AudioObjectPropertyAddress) { 
         if (kAudioHardwareNoError != AudioObjectAddPropertyListenerBlock(onAudioObjectID, &forPropertyAddress, nil, listenerBlock)) { 
          print("Error calling: AudioObjectAddPropertyListenerBlock") } 
        } 
    
    
        override func viewDidLoad() { super.viewDidLoad() 
    
         addListenerBlock(audioObjectPropertyListenerBlock, 
         onAudioObjectID: AudioObjectID(bitPattern: kAudioObjectSystemObject), 
         forPropertyAddress: AudioObjectPropertyAddress(
          mSelector: kAudioHardwarePropertyDefaultOutputDevice, 
          mScope: kAudioObjectPropertyScopeGlobal, 
          mElement: kAudioObjectPropertyElementMaster)) 
        } 
    
    ... 
    
  2. 提供一個監聽器模塊功能來接收通知。你給出一個數組,可能有不止一個屬性地址,這樣循環並尋找您想要的:

    func audioObjectPropertyListenerBlock (numberAddresses: UInt32, addresses: UnsafePointer<AudioObjectPropertyAddress>) { 
    
         var index: UInt32 = 0 
         while index < numberAddresses { 
          let address: AudioObjectPropertyAddress = addresses[0] 
          switch address.mSelector { 
          case kAudioHardwarePropertyDefaultOutputDevice: 
    
           let deviceID = getDefaultAudioOutputDevice() 
           print("kAudioHardwarePropertyDefaultOutputDevice: \(deviceID)") 
    
          default: 
    
           print("We didn't expect this!") 
    
          } 
          index += 1 
        } 
    
        // Utility function to get default audio output device: 
        func getDefaultAudioOutputDevice() -> AudioObjectID { 
    
         var devicePropertyAddress = AudioObjectPropertyAddress(mSelector: kAudioHardwarePropertyDefaultOutputDevice, mScope: kAudioObjectPropertyScopeGlobal, mElement: kAudioObjectPropertyElementMaster) 
         var deviceID: AudioObjectID = 0 
         var dataSize = UInt32(truncatingBitPattern: sizeof(AudioDeviceID)) 
         let systemObjectID = AudioObjectID(bitPattern: kAudioObjectSystemObject) 
         if (kAudioHardwareNoError != AudioObjectGetPropertyData(systemObjectID, &devicePropertyAddress, 0, nil, &dataSize, &deviceID)) { return 0 } 
         return deviceID 
        } 
    
    } 
    

當然,你可以簡單地添加更多的casesswitch聲明如果你想監視其他音頻設備屬性。致電addListenerBlock添加您感興趣的任何設備和屬性地址。

+0

在'let address:AudioObjectPropertyAddress = addresses [0]'這一行上,你的意思是把'index'而不是'0'? – GWRodriguez 2016-12-23 19:26:43