My intention is to capture the audio level of microphones in windows system.
I am trying the following code and Activation of IPart to get IAudioPeakMeter fails with E_NOINTERFACE.
Any solutions to the following error or any other approaches to capture the audio level of microphones in windows system is highly appreciable?
HRESULT hr;
IMMDeviceEnumerator* pEnumerator = NULL;
IMMDevice* pDevice = NULL;
static float peak = 0;
CoInitialize(NULL);
// Get enumerator for audio endpoint devices.
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),
NULL, CLSCTX_INPROC_SERVER,
__uuidof(IMMDeviceEnumerator),
(void**)&pEnumerator);
DisplayMessageOnError(L"CoCreateInstance", hr);
hr = pEnumerator->GetDefaultAudioEndpoint(eCapture, eCommunications, &pDevice);
DisplayMessageOnError(L"GetDefaultAudioEndpoint", hr);
CComPtr<IDeviceTopology> topology;
hr = pDevice->Activate(__uuidof(IDeviceTopology), CLSCTX_ALL, NULL, (void**)&topology);
DisplayMessageOnError(L"Activate", hr);
CComPtr<IConnector> connector;
hr = topology->GetConnector(0, &connector);
DisplayMessageOnError(L"GetConnector", hr);
CComPtr<IConnector> connectedTo;
hr = connector->GetConnectedTo(&connectedTo);
DisplayMessageOnError(L"GetConnectedTo", hr);
//CComPtr<IPart> part;
//hr = connectedTo->QueryInterface(&part);
CComPtr<IPart> part = NULL;
hr = connectedTo->QueryInterface(__uuidof(IPart), (void**)&part);
DisplayMessageOnError(L"QueryInterface", hr);
CComPtr<IAudioPeakMeter> audio;
**//Below code fails with E_NOINTERFACE.**
hr = part->Activate(CLSCTX_INPROC_SERVER, __uuidof(IAudioPeakMeter), (void**)&audio);
DisplayMessageOnError(L"part->Activate", hr);
audio->GetLevel(0,&peak);
std::wstringstream wstr;
wstr << L"GetPeakValue " << peak << '\n';
OutputDebugStringW(wstr.str().c_str());
return S_OK;