Is there a way of muting sound from my computer everytime I unplug my headphones (Like a phone does) to stop sound then playing out of my speakers?
-
1Related Link: unix.stackexchange.com/questions/25776/…– con-f-useMay 8, 2012 at 13:10
-
Related Link: unix.stackexchange.com/questions/25776/… and askubuntu.com/questions/23508/…– con-f-useMay 8, 2012 at 13:17
-
I posted an answer without using infinite loops here - askubuntu.com/a/1005144/470017– Al.G.Feb 11, 2018 at 14:56
4 Answers
Howto detect an unplug
Basically what worked for me was:
# 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
For me the difference was in 'Node 0x16' under 'Amp-Out vals':
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]
So I based the detection on the difference found.
Howto mute
With this knowledge you can have a script running in the background. If unplugged the scripts mutes your speakers like using amixer sset Master playback 0%
(or any other command).
#!/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
You can make it executable with chmod +x scriptname.sh
and put it in the startup applications. You will have to adjust the unplug detection though by finding your own difference in /proc/asound/card0/codec#0
(maybe even change the numbers here for multiple soundcards.
Related Links:
https://wiki.ubuntu.com/Audio/PreciseJackDetectionTesting
https://unix.stackexchange.com/questions/25776/detecting-headphone-connection-disconnection-in-linux
How to automatically change volume level when un-/plugging headphones?
-
-
5Having a script with an infinite
while
loop (without even a little sleep instruction) running continuously in the background is far from an ideal solution; it's an ugly and hacky workaround, in addition to being a cpu and battery killer. I tried it and went from a normal situation of constant 5% cpu utilization (with browser, spotify, terminal, IDE, Telegram, and other apps open) to 45% constant cpu use.– LeartSAug 22, 2017 at 16:06 -
This solution is a horribly bad citizen, as @LearlS points out. Please don't ever do this. For a good-citizen solution, use
acpi_listen
, as suggested in one of the links in this answer. Jun 2, 2018 at 12:25
This worked for me on Ubuntu 14.04:
"with the headphones out mute it. Insert headphones and raise volume. Remove headphones and check for mute."
Credit: RevDrStrangelove on https://www.reddit.com/r/LifeProTips/comments/369k76/lpt_request_automaticly_mute_laptop_after_headset/
For ubuntu-16.10 I made few change in this answer.
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
-
Both this answer and the answer it came from are bad citizens. They appear to work, but they hog the system resources which doesn't scale-- run a few programs like this at the same time, and it ruins the system. Please don't ever do this. You can use one of the solutions involving acpi_listen or similar instead. Jun 2, 2018 at 12:43
If you have problems with events catching in /etc/acpi/handler.sh
see my answer. It's also without device codes as Node 0x16
.