This script will cycle to the next available audio output device. It can be tied to a hotkey to easily be triggered. This is handy, for example, for swapping between speakers and headphones.
This script will work on systems running PulseAudio or Pipewire services.
- Download the
audio-device-switch.sh
script and place it in/usr/local/bin
. - Make the script executable:
sudo chmod 755 /usr/local/bin/audio-device-switch.sh
. - Open the Keyboard Shortcuts settings page, add a new shortcut, tell it to execute
audio-device-switch.sh
, and set up your shortcut! - Install the
notify-send
library if you want to see a popup notification when the audio device switches:sudo apt install libnotify-bin
.
Feel free to modify this script and make it your own. Some ideas for customization:
Line 83 of the script calls notify-send
with the -i
flag which defines which icon is displayed. Stock icons are found in:
/usr/share/icons/gnome/32x32
/usr/share/notify-osd/icons/
This is a more modern, robust rewrite of tsvetan's solution on the Ubuntu forums.
That's great, but I had to invert line 26 with line 27 to make it work for me: you must increment
next_sink_index
before accessing thenext-sink
element in thesinks
array, right!?Also I had leading whitespaces in the string returned for
active_sink
variable (line 9), so I had to change it with:active_sink=$(pacmd list-sinks | sed -n -e 's/[[:space:]]*\*[[:space:]]index:[[:space:]]\([[:digit:]]\)/\1/p')
See https://gist.github.com/pgillet/534041a0f2ae1ae94aff2e74bf6096f1