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.

13 comments:

Lukas said...

Great, the real solution - even better than VistaAudioChanger that stopped working with my new Plantronics headset. Do you know about windows 7 gadget programming and maybe a solution for onscreen infotag?

Lukas said...

Coastal, great solution, saved me after my new headset stopped working VistaAudioChanger 2.11b with Windows 7 32-bit.
Do you know about sidebar (gadget) programming. Would like to see a possibility to display an onscreen message after changing the audio device.

r said...

Hey! The vbs files works perfectly for me, but the actual sidebar gadget does nothing. I see the buttons but nothing happens when I press them. The buttons aren't connected to the vbs files somehow.. the vbs files are in my system32 folder.

Am I supposed to change the following two lines aswell?

replaced the html specific symbols with *

*input type="button" value="Speakers" onClick="SetVistaDefaultAudio(0)"*
*input type="button" value="Headphones" onClick="SetVistaDefaultAudio(1)"*

in any case, could you please try to help me, would be really glad to make this fine solution work for me. Thanks in advance!

coastal said...

r, you'll need to edit the WshShell.Run lines in the AudioSwitch.html file. If the vbs scripts are not being found it means the folder they're in is not included in the PATH environment variable. You can fix this by either updating the PATH variable in system settings or edit the WshShell.Run line to include the path to the vbs file. I'm pretty sure that'll work.

WshShell.Run("c:\myscripts\speakers.vbs");

The Suicide King said...

Dude this is great man. I have been looking for a solution like this for ages. Just wanna let you know thank you

Guy said...

wicked man - very helpful!

I'm modifed the vbs code so it goes to the correct sound device.

on click the sound properties opens, but it just flashes, and doesn't dissapear, nor does it change the default sound device.

so i'm assuming the sendkeys command isn't working properly?

any ideas?

running on w7-64pro

cheers

coastal said...

Guy,
It sounds like your hitting on a device when that's the device that's already selected. It'll only work correctly if you hit the button for a device that you're currently not using. It's bit of a drag and i'm thinking that i should write a decent taskbar app to make it smoother.

Dave said...

Hey

I've been looking at multiple sollutions and they all include the automatisation of the windows audiocontrol pannel. Is there no way to avoid this? Like working with registry, etc.

Why did they ever think we would be happy if things became more complicated.

PS: Sorry for the thread necromancy.

Dave said...

First of sorry for the thread necromancy.

Nice sollution, the best I've seen so far (and I've tested quite a few). Ever got that gadget to work without using the audio devide window? It just bugs me that it has to pop-up.

Using this sollution till I find a better one.

coastal said...

In short, MS don't want software to automatically change devices. So they want the user to use the control panel. There's one solution I've seen where the correct registry settings are altered to change the device (outputs only) without the control panel appearing but it's certainly a hack-around and something that MS wouldn't want to see more of I guess. Pain, i know.

Disquatics said...

Some more thread necromancy here! I had the "old" gadget installed and working fine on W7 but just installed a new drive and new W7 and it did not work when copied across. Weird, as the old drive was SP1 and 100% up to date with patches.

Anyway, your thread helped get this working again, but I tweaked the vbs files by using the "old" switcher exe, rather than the keypresses/etc. My SPDIF.vbs (for example) is -

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "%windir%/system32/SetVistaDefaultAudio.exe 3 Sound S",0
Set WshShell = Nothing

It's faster and you don't get the issue of the devices box staying open if you click the same one twice.

Obviously it relies on having that exe where it says, and you have to change the index for each vbs file. Bit annoying but works like the old way... in the end! ;-)

asdfsdf said...

Hmm but if the default COMMUNICATIONS device is set, does it change it also? All default audio device changing software that Ive checked so far do this... and it is bad :(

Andrew Bailey said...

No. It handles default comms device separately from the default audio device. This applies for both input and output.