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

时间:2013-03-31 10:34:21

标签: macos audio audiounit headset speaker

使用AudioUnit和kAudioUnitSubType_HALOutput如何检测输出是扬声器还是耳机?

1 个答案:

答案 0 :(得分:2)

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