Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change volume win32 c++

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

like image 950
user37875 Avatar asked Mar 31 '09 00:03

user37875


4 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);
like image 60
Irwin Avatar answered Nov 04 '22 21:11

Irwin


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.

like image 21
ReinstateMonica Larry Osterman Avatar answered Nov 04 '22 21:11

ReinstateMonica Larry Osterman


Two options:

  1. There's an answer to that question here on SO (changing the master volume from C++, which also includes SetMute, etc.)

  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.

like image 2
Clay Nichols Avatar answered Nov 04 '22 20:11

Clay Nichols


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));
}
like image 1
mchouhan_google Avatar answered Nov 04 '22 21:11

mchouhan_google