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

发布于 2024-08-20 04:35:44 字数 88 浏览 3 评论 0原文

我想通过 JScript 或 VBScript 脚本控制 Windows 系统的音量。有什么想法吗?

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

冷弦 2024-08-27 04:35:44

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

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:

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.

方圜几里 2024-08-27 04:35:44

要使系统音量静音或取消静音,您可以使用 < code>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

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

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 NirCmd tool:

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:

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
赠意 2024-08-27 04:35:44

在 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。由于某种原因,键盘快捷键只有在桌面上才有效,所以我不确定键盘快捷键有多可靠!

On Windows 7 I could do this by controlling the keystrokes using 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!

才能让你更想念 2024-08-27 04:35:44

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

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

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
小伙你站住 2024-08-27 04:35:44

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

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

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

Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys(chr(&hAD))
风尘浪孓 2024-08-27 04:35:44

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

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

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.

水波映月 2024-08-27 04:35:44

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

I used nircmd.exe along with vbscript to control system volume.

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文