I'm trying to find the audio devices (both input and output) connected to my computer. I would like to switch my default audio out from headphones to speakers and back.
The docs feel cyclical, telling me I need to use IMMDeviceCollection to enumerate my devices, but to get the collection, I need to provide it to the IMMDevicenumerator, but it can't be empty otherwise it throws an error/warning.
The short, how do I list my audio devices? How can I switch what is considered my default audio endpoint?
TIA
void AudioControl::getDevices()
{
IMMDeviceEnumerator* deviceEnumerator = NULL;
IMMDeviceCollection* deviceCollection = NULL;
HRESULT hr = CoInitialize(NULL);
if (SUCCEEDED(hr))
{
std::cout << "Initialized" << std::endl;
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&deviceEnumerator);
if (SUCCEEDED(hr))
{
std::cout << "Created instance" << std::endl;
hr = deviceEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, (IMMDeviceCollection**)deviceCollection);
if (SUCCEEDED(hr))
{
if (deviceCollection != NULL)
{
std::cout << "Enumerated devices" << std::endl;
std::wcout << "Devices: " << deviceCollection << std::endl;
std::wcout << "Type: " << typeid(deviceCollection).name() << std::endl;
}
}
std::cout << "Done enumerating devices" << std::endl;
}
}
deviceCollection->Release();
deviceEnumerator->Release();
CoUninitialize();
}