Unfortunately playing with the registry didn't help me. I guess it is because I have a single sound card and I'm just trying to switch between 2 ports (front and back). So I went back to use the following GUI automation.
The function below takes "device" as it appears in mmsys.cpl and looks for it in the combobox. If it finds it it changes the device and returns true. Otherwise it returns false.
I do use the registry first, however, to check whether the current device is the one we are trying to switch to since, while setting the registry value doesn't change the real sound device, changing the sound device DOES reflect in the registry.
Code:
ChangeSoundDevice(device)
{
window=Sounds and Audio Devices Properties
success := false
RegRead, registryDevice, HKEY_CURRENT_USER, Software\Microsoft\Multimedia\Sound Mapper, Playback
registryDevice = %registryDevice%
if registryDevice = %device%
{
success := true
}
else
{
Run control mmsys.cpl`,`,2
WinWait, Sounds and Audio Devices Properties,,5
ControlSend,ComboBox1,{Home},%window%
CurrentDevice=
Loop
{
ControlGetText,outputs,ComboBox1,%window%
; Trimming spaces
outputs = %outputs%
if outputs = %device%
{
success := true
break
}
else if outputs<>%CurrentDevice%
{
;MsgBox, Not equal: "%CurrentDevice%" <> "%outputs%"
CurrentDevice = %outputs%
}
else
{
;MsgBox, Equal: "%CurrentDevice%" == "%outputs%"
OSD("Device not found:" . device)
break
}
ControlSend,ComboBox1,{Down},%window%
}
Send ^{Enter}
}
return success
}
Thanks,
Vitaly
P.S The code works in XP, I assume some changes would be needed under Vista/7