简体   繁体   English

更改音量 win32 C++

[英]change volume win32 c++

How would I go about changing the sound volume in c++ win32?我将如何在 c++ win32 中更改音量? Also how would I mute/unmute it?另外我将如何静音/取消静音? Thanks for the help!谢谢您的帮助!

Use the waveOutSetVolume API.使用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);

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 IAudioEndpointVolume interface.如果您想在 Vista 及更高版本上更改主音量,请搜索IAudioEndpointVolume接口。

Here's a blog post I wrote on this a couple of years ago. 这是我几年前写一篇博文。

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.想一想 - 如果我降低 MediaPlayer 中的音量,所有其他程序仍然像以前一样响亮,这正是我对任何程序的期望 - 只降低它自己的音量。 Of course there might be reasons to change global volume, no offense ;)当然,可能有理由改变全局音量,没有冒犯 ;)

Two options:两种选择:

  1. There's an answer to that question here on SO (changing the master volume from C++, which also includes SetMute, etc.) SO 上这个问题的答案(从 C++ 更改主音量,其中还包括 SetMute 等)

  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. (您基本上只是使用音量控制小程序。

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

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.以类似的方式,您可以触发 +Volume 和 -Volume 键行为。 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看看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:如果您需要更多地控制系统主音量,则必须检查 Windows 版本并执行 2 个版本的代码:
Something like aforementioned Changing master volume level for Win XP.类似于前面提到的更改Win XP 的主音量级别
Something like https://stackoverflow.com/a/3437069/1365066 for Vista and higher.对于 Vista 及更高版本,类似于https://stackoverflow.com/a/3437069/1365066

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM