I have a system with a graphic card with HDMI output and I'm often using my TV to watch movies and listen music though HDMI. Unfortunately Ubuntu doesn't switch the sound output automatically upon HDMI device connection and I had to do it manually everytime. After some research and experiments I made a little bash script that switches the output audio devices.
The script assumes that pulseaudio is used and was tested with Karmic. It is pretty generic and actually has nothing to do with any HDMI device – it simply iterates over the sound devices and switches the output to the next one.
Here is how to make it work:
- # sudo gedit /usr/local/bin/audio-device-switch.sh
- Copy and paste the source code bellow in the gedit editor
- Save it and close gedit
- # sudo chmod 755 /usr/local/bin/audio-device-switch.sh
- System -> Preferences -> Keyboard Shortcuts
- Press „Add“ and enter „Switch between audio devices“ as name and audio-device-switch.sh as command and press „Apply“.
- Select the newly added shortcut row and click on the „shortcut“ column. Choose a shortcut combination – e.g. Win + F12.
- That's all - now you can plug in your plug in your HDMI device and switch the audio output by pressing the chosen shortcut combination.
Code:
#!/bin/bash
declare -i sinks_count=`pacmd list-sinks | grep -c index:[[:space:]][[:digit:]]`
declare -i active_sink_index=`pacmd list-sinks | sed -n -e 's/\*[[:space:]]index:[[:space:]]\([[:digit:]]\)/\1/p'`
declare -i major_sink_index=$sinks_count-1
declare -i next_sink_index=0
if [ $active_sink_index -ne $major_sink_index ] ; then
next_sink_index=active_sink_index+1
fi
#change the default sink
pacmd "set-default-sink ${next_sink_index}"
#move all inputs to the new sink
for app in $(pacmd list-sink-inputs | sed -n -e 's/index:[[:space:]]\([[:digit:]]\)/\1/p');
do
pacmd "move-sink-input $app $next_sink_index"
done
#display notification
declare -i ndx=0
pacmd list-sinks | sed -n -e 's/device.description[[:space:]]=[[:space:]]"\(.*\)"/\1/p' | while read line;
do
if [ $next_sink_index -eq $ndx ] ; then
notify-send -i notification-audio-volume-high "Sound output switched to" "$line"
exit
fi
ndx+=1
done;
Hope that this is helpful.
Bookmarks