【问题标题】:How to control Windows system volume using JScript or VBScript?如何使用 JScript 或 VBScript 控制 Windows 系统音量?
【发布时间】:2017-08-10 21:12:35
【问题描述】:

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

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

【问题讨论】:

    标签: windows vbscript javascript volume mute


    【解决方案1】:

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

    【讨论】:

    【解决方案2】:

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

    【讨论】:

    • nircmd 很棒,甚至可以控制应用音量和单独的混音器控制
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      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
      

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          【解决方案6】:

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

          来自 Windows 脚本宿主(独立的 .vbs 或 .js 文件)——我不知道。根据this question,WMI 也不提供对此的控制。

          【讨论】:

            【解决方案7】:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-10-25
              • 2011-01-25
              • 1970-01-01
              • 2011-05-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多