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

10
我希望能够从JScript或VBScript脚本控制Windows系统的音量。有什么建议吗?
另外,如果系统静音了,我能否取消静音?
7个回答

10

我认为在Windows上操作系统音量的最佳方法,无需安装额外的软件,是使用VBScript以以下方式之一:

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

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

提高音量:

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

降低音量:

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

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


谢谢Ryan。你帮我省了不少时间,因为我已经在SuperUser上发布了一个指南,介绍如何使用JavaScript完成这个任务。 - TOOGAM

9

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

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

如果要从脚本中更改音量级别,有一种解决方案涉及一些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还可以静音或取消系统音量:
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

2
nircmd非常棒,甚至可以控制应用程序的音量和单独的混音器控件。 - akostadinov

2
在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.

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


2
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.

如果你想降低音量,你需要这样做:
oShell.SendKeys("{PGDN}") 'It Will Decrease The Volume By 20

1

0

从浏览器窗口/网页内部 - 绝对不可能。即使在技术上是可能的,浏览器沙箱安全性也会禁止它。

从 Windows Script Host(独立的 .vbs 或 .js 文件)内部 - 据我所知,没有任何方法。根据this question,WMI 也不能控制它。


0
我使用了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]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接