I typically have headphones permanently connected to my desktop computer but still find the need to quickly switch the audio to the desktop speakers. I’d prefer to avoid plugging/unplugging my headphones so came up with this approach.
This uses the Pulse Audio controller command: pactl
First identify your available audio outputs:
$ pactl list short | grep alsa_output | grep -v monitor
Then create some scripts to set the chosen output / sink.
Then setup some keyboard shortcuts to run the scripts. I chose ALT-SHIFT-S and ALT-SHIFT-H for speakers and headphones, respectively.
*UPDATE NOV 2022: * After upgrading to Mint 21 there needs to be an additional change to
/etc/pulse/default.pa for the CLI sink changes to work.
load-module module-stream-restore to
load-module module-stream-restore restore_device=false
Then restart pulseaudio via
pulseaudio --kill then