如何检测拔出
基本上对我有用的是:
# When plugged in:
cat /proc/asound/card0/codec#0 > pluggedin.txt
# When not plugged in:
cat /proc/asound/card0/codec#0 > notplugged.txt
# Then compare the differences
diff pluggedin.txt notplugged.txt
对我来说,区别在于“ Amp-Out vals”下的“ Node 0x16”:
Node 0x16 [Pin Complex] wcaps 0x40058d: Stereo Amp-Out Node 0x16 [PinComplex] wcaps 0x40058d: Stereo Amp-Out
Amp-Out caps: ofs=0x00, nsteps=0x00, stepsize=0x00, mute=1 Amp-Out caps:ofs=0x00, nsteps=0x00, stepsize=0x00, mute=1
Amp-Out vals: [0x80 0x80] | Amp-Out vals: [0x00 0x00]
因此,我根据发现的差异进行检测。
如何静音
有了这些知识,您就可以在后台运行脚本了。如果将其拔出,脚本将使扬声器静音,就像使用一样amixer sset Master playback 0%
(或其他任何命令)。
#!/bin/bash
# This scripts detecs unplugging headphones.
oldstatus="unrelated string"
while [ 1 ]; do
# The following line has to be changed depending on the difference (use diff) in '/proc/asound/card0/code#0'
status=$(grep -A 4 'Node 0x16' '/proc/asound/card0/codec#0' | grep 'Amp-Out vals: \[0x80 0x80\]')
if [ "$status" != "$oldstatus" ]; then
if [ -n "$status" ]; then
echo "Plugged in"
amixer sset Master playback 80% # Set volume to 80%
oldstatus="$status"
else
echo "Unplugged"
amixer sset Master playback 0% # Mute
oldstatus="$status"
fi
fi
done
您可以使其可执行chmod +x scriptname.sh
并放入启动应用程序。您必须通过找到自己的差异来调整拔出检测/proc/asound/card0/codec#0
(甚至可以更改多个声卡的数字)。
相关链接:
https://wiki.ubuntu.com/Audio/PreciseJackDetectionTesting
/unix/25776/detecting-headphone-connection-disconnection-in-linux
拔/插耳机时如何自动改变音量?