Set default audio playback device

Post your working scripts, libraries and tools for AHK v1.1 and older
Miguel7
Posts: 186
Joined: 08 Sep 2014, 07:06

Set default audio playback device

11 May 2017, 00:50

So about a month ago, a buddy hooked me up with a sweet big-screen TV, and showed me how to send my computer's display to it. But the one kind of annoying thing I had to deal with was constantly switching the default playback device from my laptop to my TV. I wanted a way to do this a bit easier, and it wasn't a super-important project (and I'm a programming geek always looking to up my game), so my first thought was C++. I explored everything from OpenAL to registry keys to third-party DLLs, and there didn't seem to be a way (Microsoft doesn't provide an API for it anymore). So then I'm like, guess I'll have to automate the stupid GUI they want you to use - oh wait, I can AutoHotKey that, duh! :)

So in about 10 minutes, I had it working. But enough with the background story - let's check out the code!

Code: Select all

/**
 * Sets the default audio playback device
 * @param[in] {int} n - A number from 1 to the total number of devices you have (3 on my machine)
 * @remarks - Tested on Win10 x64 with AHK 1.1.25.01 Unicode x86
 */
SetSound(n)
{
    ; Because this is a control panel "applet" and not a regular program, the Run call is pretty strange, but it works
    Run, rundll32.exe shell32.dll`,Control_RunDLL mmsys.cpl
    
    ; For the same reason, WinActivate, WinWaitActive etc. don't work.  So:
    Sleep 500

    ; But once the window is up, it's just straightforward Send calls the rest of the way.
    Send {Tab 4}{Down}
    Loop % n - 1
        Send {Down}
    Send {Tab 2}{Space}{Escape}
}

; And test it - worked for me
^1::SetSound(1)
^2::SetSound(2)
^3::SetSound(3)
So apart from the weird Run command, it's pretty basic stuff (nothing like what I'm using AHK for at work) but I'm sure I'm not the first person to want to do this, so I thought I'd pass it on.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Set default audio playback device

11 May 2017, 03:24

Nice one!
WinWaitActive works fine here (win7), I did a slight mod which doesn't rely on tab and returns if the device is already standard. You need to make sure there actually is text which says standard in the listview though. And you need to verify the button's classNN.

Code: Select all

SetSound(n)
{
    Run, rundll32.exe shell32.dll`,Control_RunDLL mmsys.cpl
	WinWaitActive, ahk_class #32770 ahk_exe rundll32.exe
	ControlGet, lst, List,, SysListView321, A
	lst:=strsplit(lst, "`n" )
	if !InStr(lst[n],"standard"){
		ControlClick, SysListView321, A
		Send,% "{Home}" (n-1 ? "{Down " n-1 "}":"")
		ControlClick, Button2, A
	}
	WinClose, A
	return
}

; And test it - worked for me
^1::SetSound(1)
^2::SetSound(2)
^3::SetSound(3)
^4::SetSound(4)

Miguel7 wrote:Run, rundll32.exe shell32.dll`,Control_RunDLL mmsys.cpl
Thanks for that one. On one of my laptops I sometimes have to test the sound in order for my earplugs to function, it is tedious, this is more comfortable :)

Code: Select all

TestSound()
TestSound()
{
	Run, rundll32.exe shell32.dll`,Control_RunDLL mmsys.cpl
	WinWaitActive, ahk_class #32770 ahk_exe rundll32.exe
	ControlGet, lst, List,, SysListView321, A
	lst:=strsplit(lst, "`n" )
	for n, dev in lst
		if InStr(dev,"standard")
			break
	ControlClick, SysListView321, A
	Send, % "{Home}" (n-1 ? "{Down " n-1 "}":"")
	ControlClick, Button1, A
	WinWaitActive, ahk_class NativeHWNDHost ahk_exe rundll32.exe
	ControlClick, Button4, A
	sleep, 1500
	WinClose, ahk_class NativeHWNDHost ahk_exe rundll32.exe
	WinWaitClose, ahk_class NativeHWNDHost ahk_exe rundll32.exe
	WinClose,ahk_class #32770 ahk_exe rundll32.exe
	return
}
Cheers!
passerby

Re: Set default audio playback device

11 May 2017, 17:37

Autohotkey can run the external tool NirCmd to change the sound device silently http://www.nirsoft.net/utils/nircmd.html

The code here also appears to change it silently, https://github.com/zivsha/SoundControl . Perhaps someone smarter than me can figure out a way to convert that code to a native Autohotkey script?
User avatar
tidbit
Posts: 1273
Joined: 29 Sep 2013, 17:15
Location: USA

Re: Set default audio playback device

11 May 2017, 20:12

See VA.ahk for an AHK way to do it: https://autohotkey.com/board/topic/2198 ... functions/
Currently can only be downloaded from Drugoys github: https://github.com/Drugoy/Autohotkey-sc ... ies/VA.ahk
Lexikos wrote:beep
Dropbox got rid of the public folder a few weeks ago, so the va.ahk links are dead.
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
bananacry
Posts: 4
Joined: 28 Oct 2017, 08:19

Re: Set default audio playback device

28 Oct 2017, 08:41

tidbit wrote: Currently can only be downloaded from Drugoys github: https://github.com/Drugoy/Autohotkey-sc ... ies/VA.ahk
Sadly this script version does not contain the VA_SetDefaultEndpoint("speaker", 1) function i was looking for... Does anyone have the newest (2.3) version from Lexikos which went down? (Drugoy's is 2.1)

btw I registered new to this forum and wondered that the "github" field does not support my Github name Martin-SF because of the "-" :(
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Set default audio playback device

28 Oct 2017, 08:52

Try this:
autohotkey-scripts/VA.ahk at master · vrillusions/autohotkey-scripts · GitHub
https://github.com/vrillusions/autohotk ... Lib/VA.ahk
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
bananacry
Posts: 4
Joined: 28 Oct 2017, 08:19

Re: Set default audio playback device

30 Oct 2017, 12:38

Thank you jeeswg!

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Draken, NinjoOnline, NPerovic and 187 guests