Skip to content

Instantly share code, notes, and snippets.

@Phoenix616
Last active April 13, 2020 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Phoenix616/4b6bef3fe2ad622b21527043a7acd679 to your computer and use it in GitHub Desktop.
Save Phoenix616/4b6bef3fe2ad622b21527043a7acd679 to your computer and use it in GitHub Desktop.
Switch sound devices with AutoHotKey under Windows on a button press. Supports separate media channel redirection. Uses NirSoft's NirCmd and SoundVolumeView
; Switch Sound Device by Phoenix616
; Based on work by fohrums:
; https://autohotkey.com/board/topic/68257-toggle-set-default-audio-device-in-windows-7/page-2#entry632858)
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn ; Recommended for catching common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
nirSoundVolumeViewPath := "D:\Programme\NirSoftPackage\NirSoft\SoundVolumeView.exe"
nirCmdPath := "D:\Programme\NirSoftPackage\NirSoft\NirCmd.exe"
speakers := "Lautsprecher"
headphones := "Kopfhörer"
mediaSpeakers := "Lautsprecher"
mediaHeadphones := "Kopfhörer (Passthrough)"
media := "Media"
; Force set to Lautsprecher on start
useHeadphones := false
usePlaybackDevice(speakers, mediaSpeakers)
; Toggle speaker setup, change to whatever hotkey you want (I use the media key that is available on some keyboards)
Launch_Media::
If (useHeadphones)
usePlaybackDevice(speakers, mediaSpeakers)
else
usePlaybackDevice(headphones, mediaHeadphones)
useHeadphones := !useHeadphones
return
usePlaybackDevice(device, mediaDevice) {
global media
global nirSoundVolumeViewPath
global nirCmdPath
Run, "%nirSoundVolumeViewPath%" /SetPlaybackThroughDevice "%media%" "%mediaDevice%"
Run, "%nirCmdPath%" setdefaultsounddevice %device%
; If you want to set the volumne after switching, uncomment this:
; Soundset, 50
TrayTip, , %device%, 2, 17 ; 2 seconds, info icon (1) without sound (+16)
SetTimer, removeTrayTip, 2000 ; TrayTip durations under 10 seconds don't work, remove ourselves after 2 second timer
; play a sound on the newly activated device
Sleep, 500
SoundPlay, *64 ; Asterisk (info)
}
removeTrayTip() {
SetTimer, RemoveTrayTip, Off
TrayTip ; without parameters, removes displayed traytip
}
@TheRolf
Copy link

TheRolf commented Apr 13, 2020

It seems like you need an external program (NirSoftPackage) in order for this to work?

@Phoenix616
Copy link
Author

Well not the full package but yes, the NirCmd and SoundVolumeView programs are necessary. I really didn't have the time or muse to try to figure out some of the more obscure parts of the Windows "API" (as far as I can tell it's not officially exposed) in these parts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment