如何在 Windows、Mac 和 Ubuntu 中以编程方式设置音量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1920749/
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
How to programmatically set volume in Windows, Mac and Ubuntu?
提问by Viet
I'd like to programmatically set volume in Windows, Mac and Ubuntu using C/C++. Command line also can but C/C++ preferred. Thank you in advance!
我想使用 C/C++ 以编程方式在 Windows、Mac 和 Ubuntu 中设置音量。命令行也可以,但首选 C/C++。先感谢您!
回答by Andrew
Just a hint, In windows the 'philosophy' around volume adjustment has changed between XP and Vista/7. Code that would change the master volume on XP will only change the application specific volume setting in the mixer on Vista and 7.
只是一个提示,在 Windows 中,围绕音量调节的“哲学”在 XP 和 Vista/7 之间发生了变化。在 XP 上更改主音量的代码只会在 Vista 和 7 上更改混音器中的应用程序特定音量设置。
Here is a good blog post by one of the MS audio dev team regarding this: Larry Osterman's Blog
这是 MS 音频开发团队的一个关于此的很好的博客文章:Larry Osterman 的博客
Here are some codeproject pages that might prove useful:
以下是一些可能有用的代码项目页面:
Also, there are a few powerpoint presentations regarding the new api's here: AMP Summit ppts. The Audio Endpoints in Windows Vista presentation has some good info.
此外,这里有一些关于新 api 的 PowerPoint 演示: AMP Summit ppts。Windows Vista 演示文稿中的音频端点有一些很好的信息。
As for OsX and Ubuntu, i have no idea.
至于 OsX 和 Ubuntu,我不知道。
回答by el.pescado
For Linux using ALSA sound system, you can use following command:
对于使用 ALSA 声音系统的 Linux,您可以使用以下命令:
amixer set Master 50%
回答by Max
For completeness sake, here is the OSX cli version:
为了完整起见,这里是 OSX cli 版本:
osascript -e "set volume output volume (output volume of (get volume settings)+2)"
And on the C side it's more difficult. From everything I found researching this, the easiest way is using one of the readily available objective C answers and wrapping them into a function in an extra object you can call from C/C++.
而在 C 方面则更加困难。从我发现的所有研究中,最简单的方法是使用现成的客观 C 答案之一,并将它们包装到一个可以从 C/C++ 调用的额外对象中的函数中。