KoblentsBlog Photography
Contact About
Ches
Switching audio output between headphones and HDMI in Mint / Ubuntu
I got sick of right-clicking on the sound button, selecting preferences, then selecting output device. Annoying!
So I finally got around to automating it, then even added buttons to the menu, and from there to the task bar. I'm on Linux Mint 17 (Mate) at the moment. Here's how it works, after the jump:
The following two scripts can be combined into just one script with an argument, but I wanted to be explicit / whatever.
set-output-headphones.sh
1234567
#!/bin/sh

pacmd set-default-sink 1

for index in `pacmd list-sink-inputs | grep "^\s*index" | sed -e "s/[^0-9]//g"`; do
        pacmd move-sink-input $index 1;
done
As you might imagine, set-output-hdmi is very similar.
1234567
#!/bin/sh

pacmd set-default-sink 0

for index in `pacmd list-sink-inputs | grep "^\s*index" | sed -e "s/[^0-9]//g"`; do
        pacmd move-sink-input $index 0;
done
Okay, let's explain what's going on here.
pacmd
controls the audio. Now,
pacmd list-sinks
shows which output devices are available; each output device has an index; the current one has a little star (*) next to it. In my system, HDMI is sink 0, and the headphones are sink 1.
Next,
pacmd -list-sink-inputs
shows the devices currently playing. See, setting the default sink is great, but it will only work for audio you want to play in the future. You also want to set the currently playing audio to the correct sink (output device); so we list all the audio streams, extract their indexes, and move the sink input appropriately.
This is all well and good, but we still need to create buttons for this. To do so, we need to add the following two files with sudo:
/usr/share/applications/set-sound-hdmi.desktop (feel free to change things)
12345678
[Desktop Entry]
Name=Sound To HDMI
Exec=sh /path/to/scripts/set-output-hdmi.sh
Icon=multimedia-volume-control
Terminal=false
X-MultipleArgs=false
Type=Application
Categories=System;
/usr/share/applications/set-sound-headphones.desktop
12345678
[Desktop Entry]
Name=Sound To Headphones
Exec=sh /path/to/scripts/set-output-headphones.sh
Icon=aqualung
Terminal=false
X-MultipleArgs=false
Type=Application
Categories=System;
These should now appear in your menu; you can copy the menu items to your desktop or task bar or whatever you want.
Ches Koblents
March 6, 2016
 
« Newer Older »
© Copyright Koblents.com, 2012-2024