拔下耳机时自动切换到扬声器

拔下耳机时自动切换到扬声器

目前扬声器连接到声卡,耳机连接到另一个声卡。在“输出”菜单的声音设置中,我可以看到两者,并且可以选择从哪一个获取输出。

有没有办法,当我断开耳机时,声音会自动从扬声器输出?当我重新连接耳机时,反之亦然。有点像笔记本电脑的做法。当你断开耳机时,它会自动从扬声器输出声音。

首先,当我拔下耳机时,如何检查 ubuntu 是否可以“看到”?(只是想知道这种方法是否可行)

答案1

$ 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

相关内容