Jump to content
Sign in to follow this  

Select default audio playback device

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
      $PreviousDefault = "#WARNING: Could not determine previous default device"

   $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

Share this post

Link to post
Share on other sites

Also DRAG0S, since you use such a script, you might want to take a look at the following link, where the code does the same through tray menu without automating a window.

Share this post

Link to post
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
Sign in to follow this  

  • Create New...