macos - 在Mac OS X上使用AudioUnit检测扬声器/耳机

标签 macos audio audiounit headset speaker

使用AudioUnit和kAudioUnitSubType_HALOutput如何检测输出是Speaker还是Headset?

最佳答案

bool usingInternalSpeakers()
{
    AudioDeviceID defaultDevice = 0;
    UInt32 defaultSize = sizeof(AudioDeviceID);

    const AudioObjectPropertyAddress defaultAddr = {
        kAudioHardwarePropertyDefaultOutputDevice,
        kAudioObjectPropertyScopeGlobal,
        kAudioObjectPropertyElementMaster
    };

    AudioObjectGetPropertyData(kAudioObjectSystemObject, &defaultAddr, 0, NULL, &defaultSize, &defaultDevice);

    AudioObjectPropertyAddress property;
    property.mSelector = kAudioDevicePropertyDataSource;
    property.mScope = kAudioDevicePropertyScopeOutput;
    property.mElement = kAudioObjectPropertyElementMaster;

    UInt32 data;
    UInt32 size = sizeof(UInt32);
    AudioObjectGetPropertyData(defaultDevice, &property, 0, NULL, &size, &data);

    return data == 'ispk';
}


int main(int argc, const char * argv[])
{


    if (usingInternalSpeakers())
        printf("I'm using the speakers!");
    else
        printf("i'm using the headphones!");
    return 0;
}

关于macos - 在Mac OS X上使用AudioUnit检测扬声器/耳机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15728513/

相关文章:

c++ - OS X 上有哪些动态库可以想当然?

c - 重新采样声音样本,我使用什么过滤器?

OS X 上的 Java : How to provide operations in the Services menu (NSServices)

c - 使用 XCode 构建、分析、编辑 unix\linux 控制台程序

ios - 当我尝试快速使用外部数据库和 API 时如何修复此错误

audio - 使用gstreamer进行低延迟的音频捕获

html - HTML5 Web音频API-临时录制声音

swift - AudioUnit回调与同步 : how to ensure thread safety with GCD

iphone - 使用 iOS 3d 混合器