How to control the volume of the main mixer in Windows 7 using the api mixer?

In Windows XP, it’s quite simple to control the main volume of the mixer device by setting the volume of the destination line using the api mixer. It can be easily tested using the sdk sample application provided by Microsoft. But in the case of Windows 7, whenever I open the mixing device in my application, it shows it as a new volume application, and I can only control the volume of my application. Unable to control the full sound of the system. Can anyone suggest me how to control the full sound of the speaker, which will affect the sound of all running applications.

enter image description here

How to change the speaker volume using my application in Windows 7?

+4
source share
1 answer

I believe the method you are looking for is SetMasterVolumeLevelScalar.

A quick example in C (sorry for lpVtbls):

BOOL AddMasterVolumeLevelScalar(float fMasterVolumeAdd)
{
    IMMDeviceEnumerator *deviceEnumerator = NULL;
    IMMDevice *defaultDevice = NULL;
    IAudioEndpointVolume *endpointVolume = NULL;
    HRESULT hr;
    float fMasterVolume;
    BOOL bSuccess = FALSE;

    hr = CoCreateInstance(&XIID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &XIID_IMMDeviceEnumerator, (LPVOID *)&deviceEnumerator);
    if(SUCCEEDED(hr))
    {
        hr = deviceEnumerator->lpVtbl->GetDefaultAudioEndpoint(deviceEnumerator, eRender, eConsole, &defaultDevice);
        if(SUCCEEDED(hr))
        {
            hr = defaultDevice->lpVtbl->Activate(defaultDevice, &XIID_IAudioEndpointVolume, CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
            if(SUCCEEDED(hr))
            {
                if(SUCCEEDED(endpointVolume->lpVtbl->GetMasterVolumeLevelScalar(endpointVolume, &fMasterVolume)))
                {
                    fMasterVolume += fMasterVolumeAdd;

                    if(fMasterVolume < 0.0)
                        fMasterVolume = 0.0;
                    else if(fMasterVolume > 1.0)
                        fMasterVolume = 1.0;

                    if(SUCCEEDED(endpointVolume->lpVtbl->SetMasterVolumeLevelScalar(endpointVolume, fMasterVolume, NULL)))
                        bSuccess = TRUE;
                }

                endpointVolume->lpVtbl->Release(endpointVolume);
            }

            defaultDevice->lpVtbl->Release(defaultDevice);
        }

        deviceEnumerator->lpVtbl->Release(deviceEnumerator);
    }

    return bSuccess;
}

If no GUIDs are defined:

const static GUID XIID_IMMDeviceEnumerator = { 0xA95664D2, 0x9614, 0x4F35, { 0xA7, 0x46, 0xDE, 0x8D, 0xB6, 0x36, 0x17, 0xE6 } };
const static GUID XIID_MMDeviceEnumerator = { 0xBCDE0395, 0xE52F, 0x467C, { 0x8E, 0x3D, 0xC4, 0x57, 0x92, 0x91, 0x69, 0x2E } };
const static GUID XIID_IAudioEndpointVolume = { 0x5CDF2C82, 0x841E, 0x4546, { 0x97, 0x22, 0x0C, 0xF7, 0x40, 0x78, 0x22, 0x9A } };
+4
source

All Articles