How would I go about changing the sound volume in c++ win32? Also how would I mute/unmute it? Thanks for the help!
-
WASAPI(Windows Audio Session API) would be of great use when it comes to controlling audio streams with Win32 C++. This is the link to the official documentation of the API: https://learn.microsoft.com/en-us/windows/win32/api/_coreaudio/ – Tech Trivia Mar 28 '20 at 17:20
-
Maybe you should consider to NOT change the global volume. Think about it - if I lower the volume in MediaPlayer all other programs are still as loud as before, and that is exactly what I expect from any program - to only lower it's OWN volume. Of course there might be reasons to change global volume, no offense ;) – qwerty Mar 31 '09 at 06:42
5 Answers
Use the waveOutSetVolume
API.
Here's an example:
DWORD dwVolume;
if (waveOutGetVolume(NULL, &dwVolume) == MMSYSERR_NOERROR)
waveOutSetVolume(NULL, 0); // mute volume
// later point in code, to unmute volume...
waveOutSetVolume(NULL, dwVolume);

- 499
- 1
- 3
- 12
-
4This sets the volume to zero, which is not the same as muting (although the effect is very similar). And it sets the “Wave” volume, not “Master Volume”. Which may or may not be what you want. – svick Oct 14 '11 at 10:44
-
-
Unfortunately, the waveOutGetVolume only valid for Windows below Vista. Mean, it is no more work for Vista or above. – AirCraft Lover Jul 05 '22 at 05:47
waveOutSetVolume and mixerSetControlDetails only change the volume for your application on Windows Vista and above.
If you want to change the master volume on Vista and beyond, search for the IAudioEndpointVolume interface.
Here's a blog post I wrote on this a couple of years ago.

- 16,086
- 32
- 60
-
To me "above" and "beyond" sounds almost the same when speaking about versions. Could you clarify please. – sharkin Apr 04 '09 at 07:25
-
They ARE the same. My point is that starting with Windows Vista and continuing for all subsequent versions of Windows (including Windows 7 and all subsequently released versions) the mixer and wave volumes are per- application and not global. For Vista and beyond use IAudioEndpointVolume. – Larry Osterman Apr 04 '09 at 17:41
Two options:
Have you considered showing the Volume controls and letting the user? If so, I can post some code for that. (You basically just shell out to the volume control applet.

- 1
- 1

- 11,848
- 30
- 109
- 170
If all you want to do is change the volume then you can use the virtual key codes to change volume like this:
void changeVolume()
{
INPUT ip={0};
ip.type = INPUT_KEYBOARD;
ip.ki.wVk = VK_VOLUME_UP; //or VOLUME_DOWN or MUTE
SendInput(1, &ip, sizeof(INPUT));
ip.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));
}

- 1,775
- 1
- 20
- 28
Simplest way to toggle mute is
const int APPCOMMAND_VOLUME_MUTE = 0x80000;
SendMessage(this.Handle, WM_APPCOMMAND, IntPtr.Zero, (IntPtr)APPCOMMAND_VOLUME_MUTE);
In similar way you can trigger +Volume and -Volume keys behavior. Take a look at http://www.blackwasp.co.uk/BasicVolumeControl.aspx and http://msdn.microsoft.com/en-us/library/windows/desktop/ms646247%28v=vs.85%29.aspx
There are also values for things like microphone volume control, but I haven't tried them.
If you need more control over system master volume, you must check Windows version and do 2 versions of code:
Something like aforementioned Changing master volume level for Win XP.
Something like https://stackoverflow.com/a/3437069/1365066 for Vista and higher.