Change sound output from command line
I have written a script that changes my display output from DVI (my monitor) to HDMI (my TV) by using xrandr. In this script, I would also like to change my audio output from analog line out to HDMI.
I have tried the following:
pacmd set-default-sink alsa_output.pci-0000_01_00.1.hdmi-stereo-extra1
This seems to change some sounds (like the sound you hear when you adjust volume on the desktop) to hdmi, but still the default sound output for applications (like Steam big picture mode and various games) is set to analog. To change it to HDMI, I need to click the sound settings panel on the desktop and change it manually to HDMI.
How do I change the sound output from the command line reliably? Or alternatively, what exactly does Ubuntu sound settings do under the hood when I change the sound output?
I'm running Ubuntu 15.10.
Solution 1:
Changing the default output device can indeed be done using pulseaudio's CLI interface:
pacmd set-default-sink <sink_name|index>
This default device may however be changed by the module-stream-restore which is loaded by default. This module will also restore a device unless we call it with the following parameter:
load-module module-stream-restore restore_device=false
This should be put in our default.pa
to replace the corresponding line there.
To make this change valid for all users we therefore need to edit the /etc/pulse/default.pa
as root. A better approach is however to work on a copy of this file in our user directory where we can put it to ~/.config/pulse/
cp /etc/pulse/default.pa ~/.config/pulse/
Setting we made there will override those in /etc/pulse
, so it is safe to experiment here. If anything goes wrong we simply delete this file to let pulseaudio use the system-wide defaults again.
All changes in default.pa
ne a reset of the pulseaudio server to take effect. This can be done with:
pulseaudio -k
Also see:
How to change pulseaudio sink with "pacmd set-default-sink" during playback?