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

默认音频输出-获取设备更改通知?(CoreAudio、Mac OS X、AudioHardwareAddPropertyListener),audio,listener,core-audio,Audio,Listener,Core Audio,我正在尝试使用CoreAudio API编写一个侦听器,用于更改默认音频输出(例如:插入耳机插孔)。我发现了示例代码,尽管有点旧,并且使用了不推荐使用的函数(http://developer.apple.com/mac/library/samplecode/AudioDeviceNotify/Introduction/Intro.html,但它不起作用。使用AudioHardwareAddPropertyListener方法以“正确”的方式重新编写代码,但似乎仍然不起作用。当我插入耳机时,我注册

我正在尝试使用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");

在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 );
}
添加此代码后,我甚至不需要自己注册侦听器。

尝试
CFRunLoopRun()
-它具有相同的效果。即,确保调用侦听器的事件循环正在运行