Sunday, August 3, 2008

Switching sound device with your remote

Playing a gameWelcome to more autohotkey (AHK) goodness. Building on my previous post about switching audio streams with your remote we now add switching the default audio device.

You could use any remote button you want for this, I used the hash key (#) which I don't use for anything else. I assume you already have AHK setup properly and a script for your remote. Before we continue we need to install two things for AHK:
  1. COM standard library
  2. Vista Audio/Volume Control Functions
Download both packages and extract them in the folder "My Documents\AutoHotkey\Lib\" (create it if you don't have it already).

Now you need to modify your remote script and add the following code:
;---------------------------------------------------------------------------
; When pressing #:
; Close Media Center, change default audio device and restart Media Center
;---------------------------------------------------------------------------

#IfWinActive ahk_class eHome Render Window
#::
; Close application with Alt-F4
Send !{F4}

; Change default audio
COM_Init()
device := VA_GetDevice()
device_name := VA_GetDeviceName(device)
COM_Release(device)
COM_Term()

Run, mmsys.cpl

WinWait, Ljud ahk_class #32770

if InStr(device_name, "HDMI-enhet")
ControlSend, SysListView321, {Down 2} ; second device
else
ControlSend, SysListView321, {Down 4} ; fourth device

ControlClick, Button2, Ljud ahk_class #32770 ; set default
ControlClick, Button4, Ljud ahk_class #32770 ; OK

; It might happen that clicking 'OK' doesn't work
IfWinExist, Ljud ahk_class #32770
{
WinClose, Ljud ahk_class #32770 ; close window
}

; Start Windows Media Center
Run, C:\Windows\ehome\ehshell.exe, , max

Return
#IfWinActive

Note that you need to replace all occurences of "Ljud" with your local translation of "Sound" (i.e. the name of the windows with sound properties). You might need to change the path to your ehshell as well. Restart the script and presto, you can now switch the default audio device with your remote too.

Main source of information: Lexikos work as detailed in select sound source thread on AHK forums.

No comments: