How to switch sound output with key shortcut
Check for port names
pactl list sinks
(I remove non needed sinks output):Sink #1 State: RUNNING Name: alsa_output.pci-0000_00_1b.0.analog-stereo Description: Built-in Audio Analog Stereo Driver: module-alsa-card.c ... Ports: analog-output-speaker: Speakers (priority: 10000, not available) analog-output-headphones: Headphones (priority: 9000, available) Active Port: analog-output-headphones Formats: pcm
Set sink port using
pactl set-sink-port
:pactl set-sink-port 1 analog-output-speaker
or
pactl set-sink-port 1 analog-output-headphones
If you are using a removable device (Example: USB devices), it's better to use sink
name
instead ofid
. For example:pactl set-sink-port alsa_output.pci-0000_00_1b.0.analog-stereo analog-output-headphones
Reference: man pactl
Automated solution https://ubuntuforums.org/showthread.php?t=1370383 It works on Ubuntu 18.04
Open the terminal and type:
sudoedit /usr/local/bin/audio-device-switch.sh
Copy and paste the below code in nano editor
Save it and close nano editor.
sudo chmod 755 /usr/local/bin/audio-device-switch.sh
System -> Preferences -> Keyboard Shortcuts
Press Add and enter Switch between audio devices as name and audio-device-switch.sh as command and press Apply.
Select the newly added shortcut row and click on the shortcut column. 8. Choose a shortcut combination – e.g. Win + F12.
That's all - now you can plug in your plug in your HDMI device and switch the audio output by pressing the chosen shortcut combination.
Code:
#!/bin/bash
declare -i sinks_count=`pacmd list-sinks | grep -c index:[[:space:]][[:digit:]]`
declare -i active_sink_index=`pacmd list-sinks | sed -n -e 's/\*[[:space:]]index:[[:space:]]\([[:digit:]]\)/\1/p'`
declare -i major_sink_index=$sinks_count-1
declare -i next_sink_index=0
if [ $active_sink_index -ne $major_sink_index ] ; then
next_sink_index=active_sink_index+1
fi
#change the default sink
pacmd "set-default-sink ${next_sink_index}"
#move all inputs to the new sink
for app in $(pacmd list-sink-inputs | sed -n -e 's/index:[[:space:]]\([[:digit:]]\)/\1/p');
do
pacmd "move-sink-input $app $next_sink_index"
done
#display notification
declare -i ndx=0
pacmd list-sinks | sed -n -e 's/device.description[[:space:]]=[[:space:]]"\(.*\)"/\1/p' | while read line;
do
if [ $next_sink_index -eq $ndx ] ; then
notify-send -i notification-audio-volume-high "Sound output switched to" "$line"
exit
fi
done