Wednesday 2 December 2009

Switching Audio Devices in Windows 7

/update

I've just released Coastal Audio Changer. This neat little tool will set in your system tray and swap between audio devices with a couple of clicks of the button.


The scripts I've placed below certainly work but they don't work if you hit the corresponding button of your current device. Selecting a different device is fine but if you hit the current device's button the audio control panel won't disappear. That's a little messy and I knew I could do better.

/update over

Those that were use to VistaAudioChanger or STADS in XP to swap between audio devices may have found that like me there was no working version for Windows 7.

Some simple modifications to [Kevin Lo's vbscripts] and [VistaGadgetAudioSwitch] and i've got a working gadget that swaps audio devices.

Instructions
Grab audio switch and drop it's folder into C:\Users\You\AppData\Local\Microsoft\Windows Sidebar\Gadgets

Load up the AudioSwitch.html in notepad and add height:130px; to the css body definition
and the javascript should look like this

var WshShell = new ActiveXObject("WScript.Shell");
function SetAudio(index)
{
switch(index)
{
case 0:
WshShell.Run("Speakers.vbs");
break;
case 1:
WshShell.Run("Headphones.vbs");
break;
default:
break;
}
}

i have two buttons defined, first one is speakers with an onclick value of SetAudio(0), a second headphones button has an onclick value of SetAudio(1)

I would have pasted the complete code but bloody blogger won't allow the html to be displayed - got some reading to do of my own.

Now you have to create the two vbs scripts for headphones and speakers.

My speakers.vbs looks like this (thanks Kevin);
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "%windir%/system32/control.exe /name Microsoft.Sound"
WScript.Sleep(1000)
WshShell.SendKeys "{DOWN}"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{ENTER}"
WScript.Sleep(1000)
WshShell.SendKeys "{ESC}"
Set WshShell = Nothing

The above script selects the first option in your sound devices list, sets it as the default sound device and then quits the window. You can test the file is correct by just double clicking on it and checking the results. My headphones is the third device so i have an extra couple of WshShell.SendKeys "{DOWN}" before the {TAB} lines and that's named headphones.vbs.

Drop both files to your windows\system32 folder and you're ready to launch your desktop gadget. That's right click on the desktop and select Gadgets. Drag Audio Switch to the desktop and you should have a working audio switcher.