默认音频输出 - 获取设备更改通知?(CoreAudio,Mac OS X,AudioHardwareAddPropertyListener)

OCa*_*los 3 audio core-audio listener

我正在尝试使用CoreAudio API编写一个监听器,用于更改默认音频输出(例如:插入耳机插孔).我找到了示例代码,虽然有点旧并且使用了已弃用的函数(http://developer.apple.com/mac/library/samplecode/AudioDeviceNotify/Introduction/Intro.html,但它没有用.重新编写代码使用AudioHardwareAddPropertyListener方法以'正确'的方式,但它似乎仍无法正常工作.当我插入耳机时,我注册的功能没有被触发.我在这里有点亏...我怀疑问题可能在其他地方,但我无法弄清楚在哪里......

监听器注册码:

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

OCa*_*los 6

在sourceforge中搜索使用CoreAudio API的项目之后,我找到了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 );
}
Run Code Online (Sandbox Code Playgroud)

添加此代码后,我甚至不需要自己注册一个监听器.