更改音量 win32 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/699603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:49:31  来源:igfitidea点击:

change volume win32 c++

c++winapiaudiovolume

提问by user37875

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

我将如何在 c++ win32 中更改音量?另外我将如何静音/取消静音?谢谢您的帮助!

采纳答案by Clay Nichols

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.

  1. SO 上这个问题的答案(从 C++ 更改主音量,其中还包括 SetMute 等)

  2. 您是否考虑过显示音量控件并让用户使用?如果是这样,我可以为此发布一些代码。(您基本上只是使用音量控制小程序。

回答by Irwin

Use the waveOutSetVolumeAPI.

使用waveOutSetVolumeAPI。

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);

回答by ReinstateMonica Larry Osterman

waveOutSetVolume and mixerSetControlDetails only change the volume for your application on Windows Vista and above.

waveOutSetVolume 和 mixSetControlDetails 仅更改您在 Windows Vista 及更高版本上的应用程序的音量。

If you want to change the master volume on Vista and beyond, search for the IAudioEndpointVolumeinterface.

如果您想在 Vista 及更高版本上更改主音量,请搜索IAudioEndpointVolume接口。

Here'sa blog post I wrote on this a couple of years ago.

这是我几年前写一篇博文。

回答by qwerty

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 ;)

也许您应该考虑不更改全局音量。想一想 - 如果我降低 MediaPlayer 中的音量,所有其他程序仍然像以前一样响亮,这正是我对任何程序的期望 - 只降低它自己的音量。当然,可能有理由改变全局音量,没有冒犯 ;)

回答by maximus

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));
}

回答by GrayFace

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.aspxand http://msdn.microsoft.com/en-us/library/windows/desktop/ms646247%28v=vs.85%29.aspx

以类似的方式,您可以触发 +Volume 和 -Volume 键行为。看看http://www.blackwasp.co.uk/BasicVolumeControl.aspxhttp://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 levelfor Win XP.
Something like https://stackoverflow.com/a/3437069/1365066for Vista and higher.

如果您需要更多地控制系统主音量,则必须检查 Windows 版本并执行 2 个版本的代码:
类似于前面提到的更改Win XP 的主音量级别。对于 Vista 及更高版本,
类似于https://stackoverflow.com/a/3437069/1365066