【问题标题】:Default Audio Output - Getting Device Changed Notification? (CoreAudio, Mac OS X, AudioHardwareAddPropertyListener)默认音频输出 - 获取设备更改通知? (CoreAudio、Mac OS X、AudioHardwareAddPropertyListener)
【发布时间】:2012-03-29 07:13:24
【问题描述】:

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

【问题讨论】:

    标签: audio listener core-audio


    【解决方案1】:

    在 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 );
    }
    

    添加此代码后,我什至不需要自己注册监听器。

    【讨论】:

      【解决方案2】:

      试试CFRunLoopRun() - 效果一样。即确保调用监听器的事件循环正在运行。

      【讨论】:

        猜你喜欢
        • 2021-07-27
        • 2014-11-22
        • 1970-01-01
        • 2020-06-03
        • 2013-04-03
        • 1970-01-01
        • 2021-11-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多