Javascript 如何使用 JScript 或 VBScript 控制 Windows 系统音量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2216334/
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-22 23:16:47  来源:igfitidea点击:

How to control Windows system volume using JScript or VBScript?

windowsvbscriptjavascriptvolumemute

提问by Sibo Lin

I want to control the volume of my Windows system from a JScript or VBScript script. Any ideas?

我想从 JScript 或 VBScript 脚本控制我的 Windows 系统的音量。有任何想法吗?

Also, can I unmute the system volume if it is muted?

另外,如果系统音量被静音,我可以取消静音吗?

采纳答案by Helen

To mute or unmute the system volume, you can simulate the Mute key press using the WshShell.SendKeysmethod:

要使系统音量静音或取消静音,您可以使用以下WshShell.SendKeys方法模拟静音按键:

var oShell = new ActiveXObject("WScript.Shell");
oShell.SendKeys(Chr(&HAD));

As for changing the volume level from a script, there's a solution that involves some Windows automation, such as launching the System Volume applet and simulating the appropriate keyboard shortcuts in it, but I don't think it's reliable. Therefore I recommend that you use some external utility capable of changing the volume level, and call it from your script. For example, you could use the free NirCmdtool:

至于从脚本更改音量级别,有一个解决方案涉及一些 Windows 自动化,例如启动系统音量小程序并在其中模拟相应的键盘快捷键,但我认为它不可靠。因此,我建议您使用一些能够更改音量级别的外部实用程序,并从您的脚本中调用它。例如,您可以使用免费的NirCmd工具:

var oShell = new ActiveXObject("WScript.Shell");

// Increase the system volume by 20000 units (out of 65535)
oShell.Run("nircmd.exe changesysvolume 20000");

// Decrease the system volume by 5000 units
oShell.Run("nircmd.exe changesysvolume -5000");

NirCmd can also mute or unmute the system volume:

NirCmd 还可以将系统音量静音或取消静音:

var oShell = new ActiveXObject("WScript.Shell");
oShell.Run("nircmd.exe mutesysvolume 0");  // unmute
oShell.Run("nircmd.exe mutesysvolume 1");  // mute
oShell.Run("nircmd.exe mutesysvolume 2");  // switch between mute and unmute

回答by Ryan

The best way I can see for manipulating the system volume level on Windows without the need for installing additional software is to use VBScript in one of the following ways:

我可以看到在无需安装其他软件的情况下在 Windows 上操纵系统音量级别的最佳方法是通过以下方式之一使用 VBScript:

Toggle muted: (already mentioned in a previous answer)

切换静音:(在之前的回答中已经提到)

Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys(chr(&hAD))

Increase volume level:

提高音量:

Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys(chr(&hAF))

Decrease volume level:

降低音量:

Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys(chr(&hAE))

This should work for most modern Windows machines. I've tested this on Windows 7 and 8.1 and it works fine even when run with "do as" in LC, so it is possible to embed these scripts within an executable and run them without the need of saving them as individual files.

这应该适用于大多数现代 Windows 机器。我已经在 Windows 7 和 8.1 上对此进行了测试,即使在 LC 中使用“do as”运行它也能正常工作,因此可以将这些脚本嵌入到可执行文件中并运行它们,而无需将它们保存为单独的文件。

回答by Jonco98

Misty Manor's Answer Works on Windows Vita as well. This, i literally just made, does the same in a sense.

Misty Manor 的答案也适用于 Windows Vita。从字面上看,这在某种意义上也是一样的。

set oShell = CreateObject("WScript.Shell") 
oShell.run"%SystemRoot%\System32\SndVol.exe" 'Runs The Master Volume App.
WScript.Sleep 1500 'Waits For The Program To Open
oShell.SendKeys("{PGUP}") 'Turns Up The Volume 20, If It Is Muted Then It Will Unmute It
oShell.SendKeys("{PGUP}") 'Turns Up The Volume 20
oShell.SendKeys("{PGUP}") 'Turns Up The Volume 20
oShell.SendKeys("{PGUP}") 'Turns Up The Volume 20
oShell.SendKeys("{PGUP}") 'Turns Up The Volume 20
oShell.SendKeys"%{F4}"  ' ALT F4 To Exit The App.

If you want to decrease the volume you would do

如果你想降低音量,你会这样做

oShell.SendKeys("{PGDN}") 'It Will Decrease The Volume By 20

回答by MistyManor

On Windows 7 I could do this by controlling the keystrokes using WScript.

在 Windows 7 上,我可以通过使用 WScript 控制击键来做到这一点。

set oShell = CreateObject("WScript.Shell") 
oShell.run"%SystemRoot%\System32\SndVol.exe" 
WScript.Sleep 1500 
oShell.SendKeys"{TAB} " ' Tab to the mute and press space
oShell.SendKeys"%{F4}"  ' ALT F4 to exit the app.

I saved it in to a file called Mute-Sound.vbs and created a shortcut on the desktop to assign a shortcut key. CTRL+ALT+F12. For some reason the keyboard shortcuts only work if they are on the desktop so I am not sure how reliable keyboard shortcuts are!

我将它保存到一个名为 Mute-Sound.vbs 的文件中,并在桌面上创建了一个快捷方式来分配一个快捷键。CTRL+ ALT+ F12。出于某种原因,键盘快捷键只有在桌面上才有效,所以我不确定键盘快捷键有多可靠!

回答by macxcool

http://www.nilpo.com/2008/11/windows-xp/mute-sound-volume-in-wsh/He uses a keystroke of a Multimedia Keyboard mute key. Clever ;-)

http://www.nilpo.com/2008/11/windows-xp/mute-sound-volume-in-wsh/他使用多媒体键盘静音键的击键。聪明的 ;-)

Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys(chr(&hAD))

回答by Tomalak

From within a browser window/web page - definitely no way at all. Browser sandbox security would prohibit it even if it were technically possible.

从浏览器窗口/网页中 - 绝对没有办法。即使技术上可行,浏览器沙箱安全也会禁止它。

From within Windows Script Host (standalone .vbs or .js file) - no way that I am aware of. WMI does not provide control over this either, according to this question.

从 Windows 脚本宿主(独立的 .vbs 或 .js 文件)中 - 我不知道。根据这个问题,WMI 也不提供对此的控制。

回答by Martin

I used nircmd.exealong with vbscript to control system volume.

我使用nircmd.exe和 vbscript 来控制系统音量。

Set objShell = CreateObject("WScript.Shell") 
If Not WScript.Arguments.Named.Exists("elevate") Then
  CreateObject("Shell.Application").ShellExecute WScript.FullName _
    , """" & WScript.ScriptFullName & """ /elevate", "", "runas", 1
  WScript.Quit
End If
objShell.Run("nircmd.exe setvolume 0 0 0") 

'Set WshShell = WScript.CreateObject("WScript.Shell")
'WshShell.Popup "Success", "5", "MuteSpeaker"

' to successfully run this vbscript during log-off, nircmd.exe during should be present in "C:\Windows\System32"
' [cscript //X scriptfile.vbs MyArg1 MyArg2]