我希望能够从JScript或VBScript脚本控制Windows系统的音量。有什么建议吗?
另外,如果系统静音了,我能否取消静音?
另外,如果系统静音了,我能否取消静音?
我认为在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”运行,它也可以正常工作,因此可以将这些脚本嵌入可执行文件中并在不需要将它们保存为单独文件的情况下运行它们。
要静音或取消静音系统音量,您可以使用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");
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
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。由于某种原因,只有在桌面上才能使用键盘快捷键,所以我不确定键盘快捷键有多可靠!
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
http://www.nilpo.com/2008/11/windows-xp/mute-sound-volume-in-wsh/ 他使用多媒体键盘静音键的按键。聪明;-)
Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys(chr(&hAD))
从浏览器窗口/网页内部 - 绝对不可能。即使在技术上是可能的,浏览器沙箱安全性也会禁止它。
从 Windows Script Host(独立的 .vbs 或 .js 文件)内部 - 据我所知,没有任何方法。根据this question,WMI 也不能控制它。
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]