Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Switching Playback Device


  • Please log in to reply
8 replies to this topic
TomWolf
  • Members
  • 8 posts
  • Last active: Nov 08 2014 09:57 PM
  • Joined: 25 Oct 2009

UPDATE:

Based on tips from users ctrl and Kon I've updated the script using Vista Audio Control Functions as library.

Use win + h or click the tray icon to switch between the different devices. For me the devices are 3 and 6, experiment to find what you want. I've set my headset (3) as default.
The script now requires AHK 1.1 found on ahkscript.org

Menu, Tray, Icon, %A_WorkingDir%\Icons8-Ios7-Music-Headset.ico,,1
VA_SetDefaultEndpoint("playback:" 3, 0)
VA_SetDefaultEndpoint("playback:" 3, 1)
VA_SetDefaultEndpoint("playback:" 3, 2)

#include VA.ahk

#Persistent
Menu, Tray, NoStandard
Menu, Tray, Add, &Switch Playback Device, #h
Menu, Tray, Add, 
Menu, Tray, Standard
Menu, Tray, Default, &Switch Playback Device
Return

#h::     ; Win+h toggles headphones / speakers
Toggle := !Toggle
VA_SetDefaultEndpoint("playback:" (Toggle ? 6 : 3), 0)
VA_SetDefaultEndpoint("playback:" (Toggle ? 6 : 3), 1)
VA_SetDefaultEndpoint("playback:" (Toggle ? 6 : 3), 2)
setIcon((Toggle ? 6 : 3))
return

setIcon(Toggle) {
if (Toggle == 3) {
Menu, Tray, Icon, %A_WorkingDir%\Icons8-Ios7-Music-Headset.ico,,1
}else if (Toggle == 6){
Menu, Tray, Icon, %A_WorkingDir%\Iconsmind-Outline-Speaker-2.ico,,1
}
}

OLD POST:

 

So, this is a script to change the playback device between speakers and headphones. You can customize it to suit your needs.

 

I've used two icons that are placed in the same directory as the script. You'll have to get your own icons and change the names.

 

For me the speaker is the first device in my list and headphones are the 3d, change the numbers to suit you.

 

When the program loads it will, load the 3d device, I've noted where you change that.

 

Ctrl + Win + F1 = Switch device

Double Click the systray icon = Switch device

Ctrl + Win + F2 = Show tooltip of current device

 

The system tray icon will change to the corresponding device if you have icons set.

; First we change the playback device to headphones

    Run, mmsys.cpl
    WinWaitActive, Sound ahk_class #32770
    ControlSend, SysListView321,{Down 3}, Sound ahk_class #32770 ; Down 3 changes to the 3d device, in my case headphones.
    ControlClick, Button2, Sound ahk_class #32770
    WinClose, Sound ahk_class #32770
    Menu, Tray, Icon, %A_WorkingDir%\Icons8-Ios7-Music-Headset.ico,,1

; Lets us doubleclick the tray icon to swithc between playback devices
#Persistent
Menu, Tray, NoStandard
Menu, Tray, Add, &Switch Playback Device, changePlayback
Menu, Tray, Add, 
Menu, Tray, Standard
Menu, Tray, Default, &Switch Playback Device

Return

changePlayback:
    usePlaybackDevice(1)
Return


; ctrl + win + F1 changes the device
^#F1:: 
    usePlaybackDevice(1)
return

; ctrl + win + F2 shows what device is active in a tooltip
^#F2::  
    usePlaybackDevice(2)
return

; This function changes between playback device 3 and 1. You can add more if you want to. 
usePlaybackDevice(change) {
    static device = 3    
    if (change == 1){
        if(device == 3){
            device = 1
            Menu, Tray, Icon, %A_WorkingDir%\Iconsmind-Outline-Speaker-2.ico,,1
        }else if(device == 1){
            device = 3
            Menu, Tray, Icon, %A_WorkingDir%\Icons8-Ios7-Music-Headset.ico,,1
        }
    Run, mmsys.cpl
    WinWaitActive, Sound ahk_class #32770
    ControlSend, SysListView321,{Down %device%}, Sound ahk_class #32770
    ControlClick, Button2, Sound ahk_class #32770
    WinClose, Sound ahk_class #32770

    }else if (change == 2){
    TrayTip, , % if device = 1 ? "Speakers" : "Headset"
    }

}

Have fun, will come back and edit if I add stuff.

 

Tomas



TomWolf
  • Members
  • 8 posts
  • Last active: Nov 08 2014 09:57 PM
  • Joined: 25 Oct 2009

The icons I used (simply download the ico files and put in the same folder)

http://www.iconarchi...adset-icon.html

http://www.iconarchi...ker-2-icon.html



duppy
  • Members
  • 13 posts
  • Last active: Oct 31 2015 02:14 AM
  • Joined: 16 May 2013

I haven't used your script, but I just wanted to point out an alternative method, which is using a command line program called AudioEndPointController.  I think it's a litter better than opening the mmsys control panel and sending keypresses and button clicks, but you can decide. 

 

You can download it here (EXE file is in the release folder):

https://github.com/D...hive/master.zip

 

Also, this is my own simple script to use it:

Spoiler

 

 



ctrl
  • Members
  • 5 posts
  • Last active: Jan 12 2015 06:44 AM
  • Joined: 14 Sep 2014

Lexikos wrote a library of AHK funcitons called Vista Audio Control Functions. It can be used to set the audio endpoint (ie the default playback device). It does not require opening windows / clicking controls / or using other 3rd party .exe's. Using the VA functions, this toggles the default device between device "playback:1" and device "playback:2":

#h::     ; Win+h toggles headphones / speakers
Toggle := !Toggle
VA_SetDefaultEndpoint("playback:" (Toggle ? 1 : 2), 0)
return


duppy
  • Members
  • 13 posts
  • Last active: Oct 31 2015 02:14 AM
  • Joined: 16 May 2013
 

Lexikos wrote a library of AHK funcitons called Vista Audio Control Functions. It can be used to set the audio endpoint (ie the default playback device).


Ooo, even better :) Works great...thanks!

TomWolf
  • Members
  • 8 posts
  • Last active: Nov 08 2014 09:57 PM
  • Joined: 25 Oct 2009

CTRL, I've been trying to get that to work but I get an error on line 15.

 

Error: Call to nonexistent function.

 

015: ObjRelease(aev)

 

I gather that is because I run windows 8.1

 

If I got it to run I would use it and add the part of my script that adds a handy tray icon to double click for the change.



kon
  • Members
  • 1652 posts
  • Last active:
  • Joined: 04 Mar 2013

I've been trying to get that to work but I get an error on line 15.

 

Error: Call to nonexistent function.

 

015: ObjRelease(aev)

 

ObjRelease() is an AHK v1.1 function. AHK v1.1 is only available from ahkscript.org.

AHKScript.org is a new community consisting of the active AutoHotkey developer(s) as well as other enthusiasts. Unfortunately, the old autohotkey.com domain is not under the control of the developers and it continues to promote an outdated version of the software. New users are encouraged to migrate and participate at the new site.

 

Edit (04/08/2015): This site once again hosts the current version (v1.1+) of AHK.



TomWolf
  • Members
  • 8 posts
  • Last active: Nov 08 2014 09:57 PM
  • Joined: 25 Oct 2009

Thank you Kon and ctrl. Based on your tips I've updated my script and it now works a lot more smoothly. It's not the pretties script of all since I'm not very fluent in ahk but it works. =)



TomWolf
  • Members
  • 8 posts
  • Last active: Nov 08 2014 09:57 PM
  • Joined: 25 Oct 2009

Duppy, thanks for the tip about the software, I prefer to be able to do it without having to install more software though since I can save it for later use and spread it to friends more easily.