change volume win32 c++
42,089
Solution 1
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.
Solution 2
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);
Solution 3
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.
Solution 4
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));
}
Author by
user37875
Updated on July 05, 2022Comments
-
user37875 almost 2 years
How would I go about changing the sound volume in c++ win32? Also how would I mute/unmute it? Thanks for the help!
-
sharkin about 15 yearsTo me "above" and "beyond" sounds almost the same when speaking about versions. Could you clarify please.
-
Larry Osterman about 15 yearsThey 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.
-
svick over 12 yearsThis 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.
-
GrayFace almost 10 yearsSimulating key strokes for utility tasks is never a good idea.
-
2501 over 7 yearsWhy are you passing NULL to the function?
-
Zhang over 4 yearsMy request is to toggle the volume, like 30% to 100% or reverse.