获取音频输出设备(扬声器、耳机)的 GUID

Getting GUID of audio output device (speaker,headphones)

有没有办法在 java 中获取设备(扬声器、耳机)的 GUID? 我正在尝试修改注册表中的特定值,我需要这个键名实例

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render\{d348b8e8-3118-4a9c-9b43-422647b555ca}

我要找的是最后一部分 {d348b8e8-3118-4a9c-9b43-422647b555ca}。

如果无法通过 java 使用 C++ 执行此操作?

MMDevice API 允许您枚举音频设备端点并获取它们的设备 ID。您没有指定如何识别您想要 ID 的设备。我认为友好的名称就足够了。

下面是一些示例代码:

#include <mmdeviceapi.h>
#include <objbase.h>
#include <Functiondiscoverykeys_devpkey.h>
#include <iostream>

template<typename T>
struct com_pointer
{
    ~com_pointer() { ptr_->Release(); }
    operator T*() { return ptr_; }
    T* operator->() { return ptr_; }
    T** operator&() { return &ptr_; }

    T* ptr_;
};

int main()
{
    ::CoInitialize(NULL);

    com_pointer<IMMDeviceEnumerator> enumerator;

    HRESULT hr = ::CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL,
        __uuidof(IMMDeviceEnumerator), (void**)&enumerator);

    if (hr != S_OK)
        return 1;

    com_pointer<IMMDeviceCollection> devices;
    if (enumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &devices) != S_OK)
        return 1;

    UINT count;
    if (devices->GetCount(&count) != S_OK)
        return 1;

    for (UINT i = 0; i < count; ++i)
    {
        com_pointer<IMMDevice> device;
        if (devices->Item(i, &device) != S_OK)
            continue;

        com_pointer<IPropertyStore> properties;
        if (device->OpenPropertyStore(STGM_READ, &properties) != S_OK)
            continue;

        PROPVARIANT name;
        if (properties->GetValue(PKEY_Device_FriendlyName, &name) != S_OK)
            continue;

        LPWSTR id;
        if (device->GetId(&id) != S_OK)
            continue;

        std::wcout << id << " - " << name.pwszVal << "\n";

        ::CoTaskMemFree(id);
    }

    return 0;
}

这会将设备 ID 和友好名称输出到控制台。对于我的系统,它看起来像这样:

{0.0.0.00000000}.{142c47f7-085b-4dee-a343-07d95b82403e} - Line (8- LitexMedia Virtual Audio Cable (WDM))
{0.0.0.00000000}.{24d74d8e-a3a9-410c-860f-f94720d051a7} - Line (6- LitexMedia Virtual Audio Cable (WDM))
{0.0.0.00000000}.{4b5f58ca-bae3-40c8-b394-d380782f16b6} - Speakers (ASUS Xonar DG Audio Device)
{0.0.0.00000000}.{5321f69d-c2d5-4e4f-922c-7f11a8d328fd} - S/PDIF Pass-through Device (ASUS Xonar DG Audio Device)
{0.0.0.00000000}.{59acfa2f-eb45-413b-b413-ed1043613389} - SPDIF Interface (TX0) (VIA High Definition Audio)
{0.0.0.00000000}.{87468c3a-8b84-4b70-a2d0-53a620055478} - Line (3- LitexMedia Virtual Audio Cable (WDM))
{0.0.0.00000000}.{9b5da642-3573-4520-b9c1-7d90922e89a6} - Line (2- LitexMedia Virtual Audio Cable (WDM))
{0.0.0.00000000}.{a73964bd-5357-4487-821e-2b1a2f72556a} - Line (9- LitexMedia Virtual Audio Cable (WDM))
{0.0.0.00000000}.{c895c45c-9064-4d2d-a424-2c4846490f32} - Speakers (VIA High Definition Audio)
{0.0.0.00000000}.{d4cddc1f-808c-4dfc-a0a2-7dd283fad1e0} - Line (5- LitexMedia Virtual Audio Cable (WDM))
{0.0.0.00000000}.{de953a87-89a4-40b7-9e13-8c2a6cf86cfe} - Line (4- LitexMedia Virtual Audio Cable (WDM))
{0.0.0.00000000}.{e3390713-3188-409c-918b-4f8cea7e08d4} - SPDIF Interface (TX1) (VIA High Definition Audio)
{0.0.0.00000000}.{f4493833-9f5e-4929-afc3-673d12ab3010} - Line (LitexMedia Virtual Audio Cable (WDM))
{0.0.0.00000000}.{f819358a-cdb7-4898-97c0-7a9a5d3104a3} - Line (7- LitexMedia Virtual Audio Cable (WDM))