Есть ли способ отключения звука с моего компьютера каждый раз, когда я отсоединяю наушники (как это делает телефон), чтобы остановить звук и затем воспроизводить его из моих динамиков?
В основном у меня сработало:
# 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
Для меня разница была в 'Узле 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 Masterplay 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
https://unix.stackexchange.com/questions/25776/detecting-headphone-connection-disconnection-in-linux
Как автоматически изменять уровень громкости при отключении / подключении наушников?
Для ubuntu-16.10 я внес несколько изменений в этот ответ .
oldresult="Some Random String"
while [ 1 ]; do
# incase of plugged out result will contain some data
result=$(grep "EAPD 0x2: EAPD" /proc/asound/card0/codec#0)
# checking for oldresult if not same then only go inside
if [ "$oldresult" != "$result" ]; then
oldresult=$result
if [[ -z "$result" ]]; then
notify-send "Plugged In"
amixer sset Master playback 80% # Set volume to 80%
else
notify-send "Plugged Out"
amixer sset Master playback 0% # Set volume to 0%
fi
fi
done
Это сработало для меня в Ubuntu 14.04:
«при отключенных наушниках, отключите звук. Вставьте наушники и увеличьте громкость. Снимите наушники и проверьте, не отключен ли звук». https://www.reddit.com/r/LifeProTips/comments/369k76/lpt_request_automaticly_mute_laptop_after_headset/
Если у вас есть проблемы с перехватом событий в /etc/acpi/handler.sh
см. мой ответ . Это также без кодов устройства как Узел 0x16
.