获取音频输出设备(扬声器,耳机)的GUID [英] Getting GUID of audio output device (speaker,headphones)

查看:707
本文介绍了获取音频输出设备(扬声器,耳机)的GUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Java中获取设备(扬声器,耳机)的GUID?我试图修改注册表中的特定值,因此我需要此键名

Is there way to get GUID of device (speaker,headphones) in java? Im trying to modify specific value in registry and i need this key name for instance

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

我最后要寻找的是{d348b8e8-3118-4a9c-9b43-422647b555ca}.

Last part {d348b8e8-3118-4a9c-9b43-422647b555ca} is what im looking for.

如果无法通过Java用C ++做到这一点呢?

推荐答案

The MMDevice API allows you to enumerate audio device endpoints and get their device IDs. You didn't specify how you are identifying which device you want the ID for. I've assumed the friendly name will be enough.

这是一些示例代码:

#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和友好名称.对于我的系统,它看起来像这样:

This will spit out the device IDs and friendly names to the console. For my system it looks like this:

{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))

这篇关于获取音频输出设备(扬声器,耳机)的GUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆