1

Currently speakers are connected to a sound-card and headphones are connected to another. In the sound settings in the Output menu I can see both and I can choose which one to get output from.

Is there a way that when I disconnect the headphones, sound is automatically output from the speakers? And vice-versa when I reconnect the headphones. Kind of like how a laptop does it. When you disconnect headphones it automatically outputs sound from speakers.

Firstly how do I check whether ubuntu can "see" when I unplug my headphones? (just to know if the approach is possible)

5
  • If you plug your headphones into the same card as the speakers, does that work the way that you want it to?
    – heynnema
    May 7, 2017 at 16:59
  • 1
    I have a somewhat similar situation; I have an Logitec USB Soundbar and my headphones connect to the internal audio card. (Un)plugging the headphones will not switch audio back and forth. I must use a script to toggle the output. I will put the script as an answer.
    – user680858
    May 8, 2017 at 11:50
  • please do! :))) @WillemK
    – 842Mono
    May 8, 2017 at 11:51
  • @heynnema I have trouble with the drivers of the cards so it's not easy to try, thx
    – 842Mono
    May 8, 2017 at 11:51
  • I did. I put a launcher in my panel so I can toggle the audio with 1 click.
    – user680858
    May 8, 2017 at 11:55

1 Answer 1

0
$ ls -l ToggleAudioOutput
-rwxrwxr-x 1 willem willem    1845 May  7 01:32 ToggleAudioOutput

$ cat ToggleAudioOutput 
#!/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.
#
# $ 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 can differ except for the asterix (active device)
# and after (un)plugging usb audio devices.
###############################################################################

function GetSinks()
{
  Property="device.description"

  pacmd list-sinks                    | grep -e"index:" -e"${Property}" |\
    awk 'NR%2{printf "%s",$0;next;}1' | grep -v HDMI                    |\
    sed "s/^\ \ //"                   | sed "s/ index: //"              |\
    sed "s/[ \x9]*$Property = / /"    | tr -d '\"'
}

Sinks=$(GetSinks)
NrSinks=$(echo "$Sinks" | wc -l)
ActSink=$(echo "$Sinks" | grep '*' | cut -c2)

i=1
while read Line
do
  set -- $Line
  SinkNr[$i]=$1;shift
  SinkName[$i]="$@"
  ((SinkNr[$i]==ActSink)) && ((Index=i))
  ((i++))
done < <(echo "$Sinks" | tr -d '*')

((Index++))
if ((Index > NrSinks))
then
  Index=1
fi

###############################################################################
# Do the work...

pacmd set-default-sink ${SinkNr[$Index]}
notify-send "Audio output device set to:   >${SinkName[$Index]}<" -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 ${SinkNr[$Index]}
done
###############################################################################
#EOF

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .