gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:49:34 29 4
gpt4 key购买 nike

有没有办法在 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))

关于java - 获取音频输出设备(扬声器、耳机)的 GUID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35538787/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com