Is there way to get GUID of an audio output device (speaker, headphones) in Java? I'm 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}
The last part {d348b8e8-3118-4a9c-9b43-422647b555ca}
is what I'm looking for.
In case it's not possible with Java, is there any way to do this with 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.
Here is some example code:
#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;
}
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))