1

This question already has an answer here:

It'd be great if I had a software do this for me, but I guess a bash script is inevitable. However as someone that has never wrote a single line of bash code I'd prolly need a lot of help. Where to start?

marked as duplicate by Tom Brossman, user.dz, Eric Carvalho, Soren A, user535733 Oct 31 '18 at 12:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

Sorry, I don't have the right answer for you but I can offer you the next best thing:
Create a toggle to the next audio output sink with click of the mouse.

Motivation to the solution:
My system has an HDMI port+audio, a USB audio bar and the green audio socket. When USB audio is selected, my system does not detect when the audio jack of my earphones is (un)plugged. So I am unable to use hardware events and react on them.
Therefor I decided to use pacmd; a tool to manipulate the audio system.
It can be used interactively (pacmd) or with arguments (pacmd --help).

Short description:
A script and a launcher on my Xubuntu panel. A click on the launcher will toggle to the next audio output sink. If any program is playing audio, its audio input sink is forced to the output sink and notify-send is used to inform which audio output sink is active.

Actions:
The number of audio devices and their names are likely different on your system so some (minor) changes to the script are required. The comments I put in the script (further down) will help to customize it.
Copy & paste the script into a file named ToggleAudioOutput.
Make it executable. Filemanager -> Rightclik -> Permissions -> Tick Allow...
In Terminal -> Change Directory to the location of the file.

$ chmod +x ToggleAudioOutput

Launcher on Panel:
Right click the panel -> Panel > Add New Items -> Launcher.
Click Add & Close.
On the right the empty launcher appears; right click it.
Properties -> click small plus
Name: ToggleAudioOutput
Command: browse to where you stored the file and click it
Icon: select a nice one
Click Create & Close
Right-click the launcher again -> Move it to a nice spot on the panel.

Link on Desktop:
Open File Manager and browse to the location of ToggleAudioOutput.
Right-click it -> Send to Desktop (create link)

Let me know if its usefull to you !

#!/bin/bash
# -- ToggleAudioOutput -- Script to change audio output sink.
#
# The script is based on the output of this command line.
# Of course the output varies per system. Copy & paste this command line
# in a terminal to find the number of devices and their names.
#
# $ pacmd list-sinks | grep -e 'index:' -e 'alsa.name' | awk 'NR%2{printf "%s",$0;next;}1'
#   index: 0                alsa.name = "HDMI 0"
# * index: 1                alsa.name = "USB Audio"
#   index: 2                alsa.name = "ALC662 rev3 Analog"
#
# Output sequence doesn't change except for the asterix (active device)
# and after (un)plugging usb audio devices. If your system has more or
# less devices, change the case statement accordingly.
##############################################################################
# Determine the current audio output sink, 0, 1 or 2.
OutputIndex=$(pacmd list-sinks | grep '\* index:' | awk '{ print $3 }')

# To force output to USB Audio add the command:  ToggleAudioOutput 1
# in an entry in Session and Startup -> Application Autostart.
if [ "$1" == "1" ]
then OutputIndex=1; Name="USB Audio"
else
   case $OutputIndex in
   0) OutputIndex=1; Name="USB Audio"
      ;;
   1) OutputIndex=2; Name="Speaker/Headphones"
      ;;
   2) OutputIndex=0; Name="HDMI 0"
# I don't use HDMI audio, so delete this and the following line if needed.
      OutputIndex=1; Name="USB Audio"
      ;;
   esac
fi
##############################################################################
# Do the work...
pacmd set-default-sink $OutputIndex
notify-send "Audio output device set to:   >$Name<" -t 5000

# Any programs playing audio? Force them to the current audio output sink.
for InputIndex in $(pacmd list-sink-inputs | grep 'index:' | awk '{print $2}')
do
   pacmd move-sink-input $InputIndex $OutputIndex
done
##############################################################################
#EOF

Not the answer you're looking for? Browse other questions tagged or ask your own question.