【问题标题】:Detect speaker/headset using AudioUnit on Mac OS X在 Mac OS X 上使用 AudioUnit 检测扬声器/耳机
【发布时间】:2013-03-31 10:34:20
【问题描述】:

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

【问题讨论】:

    标签: macos audio audiounit headset speaker


    【解决方案1】:
    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;
    }
    

    【讨论】:

    • 有什么方法可以通知变更吗?例如我们可以在 iOS 中监听 AVAudioSessionRouteChangeNotification。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 2011-01-13
    • 2011-12-08
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多