I’m trying to mess with the audio API for a pet project of mine. I can’t seem to get the volume for the sessions.
I managed to get the default endpoint calling GetDefaultAudioEndpoint on a device enumerator.
I managed to get a IAudioEndpointVolume object to get the master volume and change it at will.
I’m trying to get the volume for each audio session using windows API.
I get a IAudioSessionManager2 then a SessionEnumerator, the session enumerator return a audioSessionControl that I use to get a simpleAudioVolume.
The simple audio volume, allows me to set the volume for the session but the GetMasterVolume returns 0.00.
Here is the code
void PrintEndpointNames() {
HRESULT hr = S_OK;
IMMDeviceEnumerator* pEnumerator = NULL;
IMMDeviceCollection* pCollection = NULL;
IMMDevice* pEndpoint = NULL;
IPropertyStore* pProps = NULL;
IAudioSessionManager2* pAudioSessionManager = NULL;
IAudioSessionEnumerator* pSessionEnumerator = NULL;
IAudioSessionManager2* pSessionManager = NULL;
IAudioEndpointVolume* ppInterface = NULL;
float volume = NULL;
PROPVARIANT id;
PROPVARIANT varName;
float pflVolumeMindB = NULL;
float pflVolumeMaxdB = NULL;
float pflVolumeIncrementdB = NULL;
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL,
CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),
(void**)&pEnumerator);
EXIT_ON_ERROR(hr, __LINE__);
// Get the default audio endpoint
hr = pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pEndpoint);
EXIT_ON_ERROR(hr, __LINE__);
hr = pEndpoint->OpenPropertyStore(
STGM_READ, &pProps);
EXIT_ON_ERROR(hr, __LINE__);
PropVariantInit(&varName);
// Get the endpoint's friendly-name property.
hr = pProps->GetValue(
PKEY_Device_FriendlyName, &varName);
EXIT_ON_ERROR(hr, __LINE__);
// GetValue succeeds and returns S_OK if PKEY_Device_FriendlyName is not found.
// In this case vartName.vt is set to VT_EMPTY.
if (varName.vt != VT_EMPTY)
{
// Print endpoint friendly name and endpoint ID.
printf("Endpoint : "%S"n", varName.pwszVal);
}
//Get audio client
pEndpoint->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL, (void**)&ppInterface);
EXIT_ON_ERROR(hr, __LINE__);
ppInterface->GetVolumeRange(&pflVolumeMindB, &pflVolumeMaxdB, &pflVolumeIncrementdB);
std::cout << " Volume range : min:" << pflVolumeMindB << "dB, max:" << pflVolumeMaxdB << "dB increment:" << pflVolumeIncrementdB << "dB" << std::endl;
ppInterface->GetMasterVolumeLevel(&volume);
printf(" CurrentVolume %fn", volume);
// Get the IAudioSessionManager2 interface
hr = pEndpoint->Activate(__uuidof(IAudioSessionManager2), CLSCTX_ALL, NULL, (void**)&pAudioSessionManager);
EXIT_ON_ERROR(hr, __LINE__);
// Enumerate audio sessions
hr = pAudioSessionManager->GetSessionEnumerator(&pSessionEnumerator);
EXIT_ON_ERROR(hr, __LINE__);
int sessionCount;
hr = pSessionEnumerator->GetCount(&sessionCount);
std::cout << " " << sessionCount << " sessions :" << std::endl;
// Looping over the sessions
for (int i = 0; i < sessionCount; i++) {
IAudioSessionControl* pSessionControl = NULL;
hr = pSessionEnumerator->GetSession(i, &pSessionControl);
EXIT_ON_ERROR(hr, __LINE__);
IAudioSessionControl2* pSessionControl2 = NULL;
pSessionControl->QueryInterface(__uuidof(IAudioSessionControl2), (void**)&pSessionControl2);
EXIT_ON_ERROR(hr, __LINE__);
float* pfLevel = NULL;
ISimpleAudioVolume* pSimpleAudioVolume = NULL;
hr = pSessionControl2->QueryInterface(__uuidof(ISimpleAudioVolume), (void**)&pSimpleAudioVolume);
pSimpleAudioVolume->GetMasterVolume(pfLevel);
printf(" ----[%d] Vol:%fn", i+1, pfLevel);
pSessionControl->Release();
}
PropVariantClear(&varName);
SAFE_RELEASE(pProps)
SAFE_RELEASE(pEndpoint)
SAFE_RELEASE(pEnumerator)
SAFE_RELEASE(pCollection)
return;
Exit:
printf("Error!n");
SAFE_RELEASE(pEnumerator)
SAFE_RELEASE(pCollection)
SAFE_RELEASE(pEndpoint)
SAFE_RELEASE(pProps)
}
int main()
{
CoInitialize(NULL);
PrintEndpointNames();
return 0;
}
Thank you