2012-03-12 155 views
2

我正在嘗試使用CoreAudio API爲默認音頻輸出更改(例如:耳機插孔插入)編寫監聽器。我發現示例代碼,雖然有點舊,並使用不推薦使用的函數(http://developer.apple.com/mac/library/samplecode/AudioDeviceNotify/Introduction/Intro.html,但它沒有工作。重新編寫代碼以正確的方式使用AudioHardwareAddPropertyListener方法,但它似乎並不奏效。當我插入耳機時,我註冊的函數沒有被觸發。我在這裏有點損失...我懷疑問題可能打下一些別的地方,但我想不通的地方......默認音頻輸出 - 獲取設備更改通知? (CoreAudio,Mac OS X,AudioHardwareAddPropertyListener)

監聽器註冊代碼:

OSStatus err = noErr; 
AudioObjectPropertyAddress audioDevicesAddress = { kAudioHardwarePropertyDefaultOutputDevice, KAudioObjectPropertyScopeGlobal, KAudioObjectPropertyElementMaster }; 
err = AudioObjectAddPropertyListener (KAudioObjectAudioSystemObject, &AudioDevicesAddress, coreaudio_property_listener, NULL); 
if (err) trace ("error on AudioObjectAddPropertyListener"); 

回答

6

對於所使用的CoreAudio的API項目在SourceForge上搜索後,我找到了rtaudio項目,更重要的是這些行:

// This is a largely undocumented but absolutely necessary 
// requirement starting with OS-X 10.6. If not called, queries and 
// updates to various audio device properties are not handled 
// correctly. 

CFRunLoopRef theRunLoop = NULL; 
AudioObjectPropertyAddress property = { kAudioHardwarePropertyRunLoop, 
            kAudioObjectPropertyScopeGlobal, 
            kAudioObjectPropertyElementMaster }; 
OSStatus result = AudioObjectSetPropertyData(kAudioObjectSystemObject, &property, 0, NULL, sizeof(CFRunLoopRef), &theRunLoop); 
if (result != noErr) { 
errorText_ = "RtApiCore::RtApiCore: error setting run loop property!"; 
error(RtError::WARNING); 
} 

添加此代碼後,我甚至不需要自己註冊監聽器。

0

嘗試CFRunLoopRun() - 它具有相同的效果。即確保正在調用監聽器的事件循環正在運行。

相關問題