Jump to content

Select default audio playback device


DRAG0S
 Share

Recommended Posts

I've written this little script for my own use, as I couldn't find anything suitable for my needs.

It contains one function which sets a new audio playback device as default (by name), and returns the name of the previous default device. This allows you to restore the previous default device after finishing whatever you have to do. I use this as part of my scripts to automate Netflix playback in XBMC; I only need to switch to a second audio device during playback, then I revert to the primary one.

The script works on my Win7 x64 by automating the Windows Sound UI. No guarantees on other platforms. :)

You can change the constants at the beginning of the script if your Windows is non-English.

Use and alter at will!

#include <GuiTab.au3>

; UI strings for English Windows locale
Global Const $WinUI_SoundWindowLabel = "Sound"
Global Const $WinUI_PlaybackDevicesTab = "Playback"
Global Const $WinUI_DefaultDeviceLabel = "Default Device"
Global Const $WinUI_SetDefaultButton = "&Set Default"
Global Const $WinUI_OKButton = "OK"

; Selects a new Windows default audio playback device
;
; Parameters:
;   $DeviceName:    Name of new audio device to set as default
;
; Return Value:
;   Success:        Name of previous default audio device. The string "#WARNING: " + warning message, if new device has been selected
;                   but some other kind of error has occurred. Currently only used when previous default device could not be determined.
;   Failure:        The string "#ERROR: " + specific error message
;
Func _AudioDevices_SetDefaultPlayback(Const $DeviceName)
   Local $PreviousDefault = "" ; Used to return the name of the previous default device
   Run(@SystemDir & "\rundll32.exe Shell32.dll,Control_RunDLL " & @SystemDir & "\mmsys.cpl") ; Open Windows Sound dialog window
   Local $Wnd = WinWait($WinUI_SoundWindowLabel, "", 20) ; Wait for up to 20 seconds for the window to show up
   If $Wnd = 0 Then Return "#ERROR: Could open " & $WinUI_SoundWindowLabel & " window"
   Local $CtlTabSwitcher = ControlGetHandle($Wnd, "", "[CLASS:SysTabControl32; INSTANCE:1]") ; Get pointer to tab list
   Local $TabIndex = _GUICtrlTab_FindTab($CtlTabSwitcher, $WinUI_PlaybackDevicesTab) ; Determine index of Playback tab
   If $TabIndex < 0 Then Return "#ERROR: Could find " & $WinUI_PlaybackDevicesTab & " tab on " & $WinUI_SoundWindowLabel & " dialog"
   _GUICtrlTab_ClickTab($CtlTabSwitcher, $TabIndex) ; Switch to Playback tab

   ; Determine current default device
   Local $DeviceIndex = ControlListView($Wnd, "", "[CLASS:SysListView32; INSTANCE:1]", "FindItem", $WinUI_DefaultDeviceLabel, 2) ; Determine index of current default device
   If $DeviceIndex >= 0 Then
      $PreviousDefault = ControlListView($Wnd, "", "[CLASS:SysListView32; INSTANCE:1]", "GetText", $DeviceIndex) ; Get name of current default device
   Else
      $PreviousDefault = "#WARNING: Could not determine previous default device"
   EndIf

   $DeviceIndex = ControlListView($Wnd, "", "[CLASS:SysListView32; INSTANCE:1]", "FindItem", $DeviceName) ; Determine index of new device to set as default
   If $DeviceIndex < 0 Then Return "#ERROR: Could find " & $DeviceName & " device in the sound devices list"
   ControlListView($Wnd, "", "[CLASS:SysListView32; INSTANCE:1]", "Select", $DeviceIndex) ; Select sound device
   ControlClick($Wnd, "", "[CLASS:Button; TEXT:" & $WinUI_SetDefaultButton & "]") ; Click "Set Default" button
   ControlClick($Wnd, "", "[CLASS:Button; TEXT:" & $WinUI_OKButton & "]") ; Click "OK" button to dismiss dialog window

   Return $PreviousDefault ; Return name of previous default device
EndFunc
Link to comment
Share on other sites

  • 5 years later...

Thanks Dragos. I'm totally new to AUTOIT (just downloaded today for this very purpose). You've saved me hours of work. I just added a call to your function within an If Then Else of based on Returns from a msgBox from the user and it worked perfect. I use a USB bluetooth send for my main speakers OR headphone plugged into my main speaker socket for playback. Depending on who's using the computer or what time it is (bed time for young ones) I needed an easy way for my wife (not too techie) to switch between outputs. Before we had the hassle of unplugging one and plugging in the other.

Thanks for all the work you've done here and for sharing it - no sense in me 'reinventing the wheel'. But i will be scouring your code to see how you did it so I can use something similar in the future to open an app and make changes.  After making the exe I added an appropriate ico image on a shortcut and pinned it to the task bar.

I think I'm gonna be using AUTOIT for quite a lot in the future - great app!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...