当前位置:  开发笔记 > 编程语言 > 正文

如何在MS WIndows 7中获取音频设备的插孔信息

如何解决《如何在MSWIndows7中获取音频设备的插孔信息》经验,为你挑选了1个好方法。



1> Simon Mourie..:

要获取插孔信息,您需要使用DeviceTopology API.

此API定义了一个IKsJackDescription接口,可为您提供插孔连接器信息.以下是一个示例控制台应用程序,它在所有渲染设备的计算机上显示此信息:

int main()
{
    HRESULT hr = S_OK;
    CoInitialize(NULL);

    CComPtr enumerator;
    hr = enumerator.CoCreateInstance(__uuidof(MMDeviceEnumerator));
    if (SUCCEEDED(hr))
    {
        CComPtr devices;
        hr = enumerator->EnumAudioEndpoints(EDataFlow::eRender, DEVICE_STATEMASK_ALL, &devices);
        if (SUCCEEDED(hr))
        {
            UINT count = 0;
            devices->GetCount(&count);
            for (int i = 0; i < count; i++)
            {
                CComPtr device;
                hr = devices->Item(i, &device);
                if (SUCCEEDED(hr))
                {
                    CComPtr store;
                    hr = device->OpenPropertyStore(STGM_READ, &store);
                    if (SUCCEEDED(hr))
                    {
                        PROPVARIANT pv;
                        PropVariantInit(&pv);
                        hr = store->GetValue(PKEY_Device_FriendlyName, &pv);
                        if (SUCCEEDED(hr))
                        {
                            PWSTR p;
                            PSFormatForDisplayAlloc(PKEY_Device_FriendlyName, pv, PDFF_DEFAULT, &p);
                            wprintf(L"name: '%s'\n", p);
                            CoTaskMemFree(p);
                        }
                        PropVariantClear(&pv);
                    }

                    CComPtr topology;
                    hr = device->Activate(__uuidof(IDeviceTopology), CLSCTX_ALL, NULL, (void**)&topology);
                    if (SUCCEEDED(hr))
                    {
                        CComPtr connector;
                        hr = topology->GetConnector(0, &connector);
                        if (SUCCEEDED(hr))
                        {
                            CComPtr connectedTo;
                            hr = connector->GetConnectedTo(&connectedTo);
                            if (SUCCEEDED(hr))
                            {
                                CComPtr part;
                                hr = connectedTo->QueryInterface(&part);
                                if (SUCCEEDED(hr))
                                {
                                    CComPtr jack;
                                    hr = part->Activate(CLSCTX_ALL, IID_PPV_ARGS(&jack));
                                    if (SUCCEEDED(hr))
                                    {
                                        UINT jackCount = 0;
                                        jack->GetJackCount(&jackCount);
                                        for (int j = 0; j < jackCount; j++)
                                        {
                                            KSJACK_DESCRIPTION desc = { 0 };
                                            jack->GetJackDescription(j, &desc);
                                            wprintf(L" jack[%i] channel mapping: %i\n", j, desc.ChannelMapping);
                                            wprintf(L" jack[%i] connection type: %i\n", j, desc.ConnectionType);
                                            wprintf(L" jack[%i] is connected: %i\n", j, desc.IsConnected);
                                            wprintf(L" jack[%i] color: 0x%08X\n", j, desc.Color);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    enumerator.Release();
    CoUninitialize();
    return 0;
}

推荐阅读
女女的家_747
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有