Setting playback device by executing a batch file / powershell script

后端 未结 6 1326
太阳男子
太阳男子 2021-01-31 04:21

I\'ve got my computer(Windows 7) hooked up to the TV, and i very often change output device for sound between Digital Audio (S/PDIF)(High definition audio device) and my headset

相关标签:
6条回答
  • 2021-01-31 05:00

    I had the exact same requirement as yourself, and AFTER stumbling across your posting I found the following:

    https://web.archive.org/web/20131231034118/http://downloadsquad.switched.com/2010/06/16/windows-7-tip-how-to-change-the-default-audio-device-with-a-hot/

    Unfortunately it's not a native Windows function; it requires the download of a small open-source scripting tool called AutoHotKey, but it works nicely and only requires a small amount of memory (1 ~ 2.5Mb)

    The script provided in the original article doesn't work for me. It's searching for Enabled/Disabled devices and changing that value, as opposed to changing the default device. I've edited it to switch between 2 default devices now. It works by opening your Sound control panel (mmsys.cpl), then scrolling down the list of playback devices to the second item in the list (that's the {Down 2} part). This is because my Speakers are the second item in my list. It then checks to see if the device is default or not. If not, it sets it as the default and closes the window. If it's already the default, it scrolls down another 2 times and sets that as the default.

    So, you'll need to ammend the {Down 2} lines to fit your own list of devices.

     #+a::
    Run, mmsys.cpl
    WinWait,Sound
    ControlSend,SysListView321,{Down 2}
    ControlGet, selectedDevice, List, Focused, SysListView321
    Loop, Parse, selectedDevice, %A_Tab%
        if a_index <> 3
            continue
        else 
        {
            if A_LoopField <> Default Device
            {
                ControlClick,&Set Default
                ControlClick,OK
                WinWaitClose
                SoundPlay, *-1
                return
            }
            else
            {
                ControlSend,SysListView321,{Down 2}
                ControlClick,&Set Default
                ControlClick,OK
                WinWaitClose
                SoundPlay, *-1
                return
        }       
    }
    
    0 讨论(0)
  • 2021-01-31 05:01

    I had a HDMI device that keeps changing it's name, so none of the existing solutions worked for me.

    I eventually ended up with this powershell and use of the NirCmd app.

    #File: TV.ps1
    $name = "SMART*"
    
    # list active audio playback devices.   (Note for cature devices change Render to Capture)
    $device = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render\*\" | where {$_."DeviceState" -eq 1} | foreach-object -Process {(Get-ItemPropertyValue -Path ($_.PSPath + "\Properties\") -Name "{a45c254e-df1c-4efd-8020-67d146a850e0},2")} | Where-Object {$_ -like $name}
    
    C:\bin\NIRCMDC setdefaultsounddevice $device 1
    C:\bin\NIRCMDC setdefaultsounddevice $device 2
    
    0 讨论(0)
  • 2021-01-31 05:02

    The following script is written in Windows 7 and uses sendkeys. It is based on other snippets I found but adds to them to ensure selection is consistent and stray windows are not left displayed. You may want to adjust SleepTime for your system if it still plays up. Call the script using a shortcut with the index of the item you wish to select in the Playback Devices window as first parameter. You can create a new 'toolbar' on your 'taskbar' to select each device with a single click: Taskbar toolbar picture

    '===============================================================================
    'This script uses sendkeys to select the Sound Output device
    'First parameter should be the desired device number in 'Playback Devices' list
    '===============================================================================
    Option Explicit
    
    Const SleepTime = 200
    
    Dim WindSh
    
    '===============================================================================
    'MAIN ROTUINE
    '===============================================================================
    'Check the command line input
    if ( Wscript.Arguments.Count <> 1)  then
      MsgBox "You must provide a single integer arguement representing the device number", vbinformation + vbokonly, Wscript.ScriptName
      Wscript.Quit 1
    elseif ( false = IsNumeric( Wscript.Arguments.Item(0) ) )  then
      MsgBox "The arguement provided was not an integer number: " & Wscript.Arguments.Item(0), vbinformation + vbokonly, Wscript.ScriptName
      Wscript.Quit 2
    End If
    
    set WindSh = CreateObject("Wscript.Shell")
    WindSh.run("control.exe mmsys.cpl")
    do while (WindSh.AppActivate("Sound") = false)
      WScript.Sleep SleepTime
    loop
    WindSh.sendkeys("{DOWN " & Clng( Wscript.Arguments.Item(0) ) & "}")
    WScript.Sleep SleepTime
    WindSh.sendkeys("{TAB 2}")
    WScript.Sleep SleepTime
    WindSh.sendkeys("{ENTER}")
    WScript.Sleep SleepTime
    WindSh.sendkeys("%{F4}")
    WScript.Sleep SleepTime
    if (WindSh.AppActivate("Sound") = true) then
      WindSh.sendkeys("%{F4}")
    end if
    
    0 讨论(0)
  • 2021-01-31 05:13

    As far as I understand there is no way to do this programmatically. This is a deliberate design, since Microsoft does not want applications to override audio setting set by user.

    You will find same answer here but if you solutions that manipulate windows you can have a look here.

    0 讨论(0)
  • 2021-01-31 05:20

    This is how I set 'Line 1' as the playback device:

    start /min "" G:\......\nircmd.exe setdefaultsounddevice "Line 1"
    

    NirCmd is a small command-line utility which you can download that allows you to do some useful tasks without displaying any user interface.

    0 讨论(0)
  • 2021-01-31 05:20

    To follow up on Dale Newton's post, NirCmd is a great way to do this. On top of that if you pair it with AutoHotKey you can create an executable that will change your devices without opening pesky CMD windows every time you run it. For example, I have two sources that I switch between all the time, one is my headphones and they other is my monitor. For my monitor I created an ahk script that does this:

    #NoEnv
    SendMode Input
    SetWorkingDir %A_ScriptDir% ;I have nircmd in the same folder as these scripts
    Run, nircmd setdefaultsounddevice "Acer X34-8" 1
    Run, nircmd setdefaultsounddevice "Acer X34-8" 2
    

    And another for my headphones with the last two lines changed to:

    Run, nircmd setdefaultsounddevice "Headset Earphone" 1
    Run, nircmd setdefaultsounddevice "Headset Earphone" 2
    

    Afterwards you can compile each ahk script into an exe and bind each exe to a keyboard macro so you can execute them with a couple key presses. Personally I am using a Corsair K95 so I use their software to bind these to my 'G' keys.

    Also to note, if you are in your sound preferences you can rename any of the devices to avoid naming conflicts.

    0 讨论(0)
提交回复
热议问题