views:

1769

answers:

4

How would I go about changing the sound volume in c++ win32? Also how would I mute/unmute it? Thanks for the help!

+1  A: 

Two options:

  1. There's an answer to that question here on SO (changing the master volume from C++, which also includes SetMute, etc.)
    http://stackoverflow.com/questions/294292/changing-master-volume-level/294525#294525

  2. 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.

Clay Nichols
+5  A: 

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);
Irwin
+1  A: 

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
+1  A: 

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.

Larry Osterman
To me "above" and "beyond" sounds almost the same when speaking about versions. Could you clarify please.
sharkin
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