In order to change the default audio output device from the command line, you can use the pacmd
Pulse Audio command-line utility.
I found the need to do similarly today, wanting to switch between headphones and speakers, both plugged into separate audio cards. Here's the shell script I wrote to do so:
#!/usr/bin/env bash
sinks=($(pacmd list-sinks | grep index | \
awk '{ if ($1 == "*") print "1",$3; else print "0",$2 }'))
inputs=($(pacmd list-sink-inputs | grep index | awk '{print $2}'))
[[ ${sinks[0]} = 0 ]] && swap=${sinks[1]} || swap=${sinks[3]}
pacmd set-default-sink $swap &> /dev/null
for i in ${inputs[*]}; do pacmd move-sink-input $i $swap &> /dev/null; done
Notes:
- This swaps between the first two audio output devices that the
pacmd
utility lists. If you have more than two audio devices and want to swap to a different one, you'll need to replace the logic on line 7 with some conditionals.
- Just swapping the default sink device does not do anything for the applications currently running — they will continue to output to the previous device. This script also moves the sink destination for all existing inputs. I.e., if you run this script with music playing on one device, it will instantly swap to the other. If you'd like the existing applications to continue on the previous device, comment out the last line (and line 5, if you'd like).
PulseAudio pacmd
is not capable of switching the default sinks while there is an actively playing stream to the sink input. However there is a way to still achieve this.
Changing default sink from command line
First we need to determine the index number of the sinks we want to switch. This can be done by calling:
pacmd list-sinks
Depending on our system this will give you a more or less lengthy list of sinks and properties that are at present accessible:
>>> 2 sink(s) available.
* index: 0
name: <alsa_output.pci-0000_01_00.1.hdmi-stereo-extra1>
driver: <module-alsa-card.c>
:
:
index: 1
name: <alsa_output.pci-0000_00_14.2.analog-stereo>
driver: <module-alsa-card.c>
The index
or the name
given here is what we need for adressing the sink by command line. The present default sink is marked with an asterix (here 0
).
To be able to switch the default sinks from command line we may need to disable stream target device restore by editing the corresponing line in /etc/pulse/default.pa
to:
load-module module-stream-restore restore_device=false
To change the default output sink to sink 1
we then run
pacmd set-default-sink 1
Sucess can be visualized by opening the Sound Settings menu.
Moving a stream to another sink
Changing the default sink while we have an active input stream playing to a given sink has no effect. This should rather be done by moving this input to another sink.
pacmd list-sink-inputs
will tell us the index of the input stream
>>> 1 sink input(s) available.
index: 5
driver: <protocol-native.c>
We now know that we want to move the input stream 5
to sink 1
by calling
pacmd move-sink-input 5 1
or back to sink 0
if we like. This will be done immediately without the need of stopping playback.
Changing default sink while playing
Of course we can combine those two commands to immediately switch default sinks during playback e.g. with
pacmd set-default-sink 1 & pacmd move-sink-input 5 1
A drawback of this method is that the input stream index changes every time we stop and restart the music player. So we always have to find out the current stream index before we can switch using the commmand line.
Best Answer
Testing Xfce panel version 4.12.2: the volume indicator already has that feature. It is a pulseaudio plugin (
xfce4-pulseaudio-plugin
) for the Xfce panel.The output device is listed under the main scroll given more than one sink is available; on mouse hover a list of available output devices is displayed.
So, no external application is needed for the purpose discussed here, although
pavucontrol
may be useful (especially given that Xfce has no specific audio settings like other desktops have).Non-Xfce-specific tools can be also used like
pasystray
(PulseAudio System Tray), as explained here, similar to Sound Switcher Indicator, already mentioned under this question;pasystray
doesn't need a new PPA to be added.