2010-02-07 75 views

回答

10

静音或取消静音系统卷,则可以使用WshShell.SendKeys方法模拟静音按键:

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

至于从脚本改变音量,还有涉及某些Windows自动化的解决方案,例如启动System Volume小应用程序并在其中模拟适当的键盘快捷键,但我认为它不可靠。因此,我建议您使用一些能够更改音量级别的外部实用程序,并从脚本中调用它。例如,你可以使用免费的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

即使对于Windows 7(32位),此功能也完美无瑕!但是,我有一个更正,因为“mutesysvolume 0”是取消静音,“mutesysvolume 1”是静音。 – 2010-02-07 10:07:27

+0

哦,对。修正了。 – Helen 2010-02-07 15:10:21

+1

nircmd真棒,甚至可以控制应用程序音量和个别调音台控件 – akostadinov 2014-01-12 19:25:13

0

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

从Windows脚本宿主(standalone .vbs或.js文件) - 没有办法,我知道。根据this question,WMI不提供对此的控制权。

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. 

我在一个叫静音,Sound.vbs文件保存它,并在桌面上创建一个快捷方式指定快捷键。 CTRL + ALT + F12。由于某些原因,键盘快捷键只有在桌面上才有效,所以我不确定键盘快捷键的可靠性如何!

1

Misty庄园的答案也适用于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

这不提供问题的答案。要批评或要求作者澄清,在他们的帖子下留下评论 - 你总是可以评论你自己的帖子,一旦你有足够的[声誉](http://stackoverflow.com/help/whats-reputation),你会能够[评论任何帖子](http://stackoverflow.com/help/privileges/comment)。 – 2014-05-04 16:48:28

+0

那里,我编辑它。 – Jonco98 2014-05-04 16:54:16

7

我可以看到用于操作在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”运行时也可以正常工作,因此可以将这些脚本嵌入到可执行文件中并运行它们,而无需将它们另存为单个文件。

+0

谢谢瑞恩。你为我节省了一些时间来创建([我在超级用户上发布的指南,如何使用JavaScript进行此操作](http://superuser.com/a/1130667/401839)) – TOOGAM 2016-10-02 15:22:58

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] 
相关问题