如何静音

如何静音

每次我拔下耳机(就像电话一样)时,有没有办法使计算机静音,从而停止从扬声器播放声音?

答案1

如何检测拔掉电源插头

基本上对我有用的是:

# 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

https://unix.stackexchange.com/questions/25776/detecting-headphone-connection-disconnection-in-linux

如何在拔下/插入耳机时自动改变音量?

答案2

这对我而言在 Ubuntu 14.04 上有效:

“取下耳机,将其静音。插入耳机并提高音量。取下耳机并检查是否静音。”

来源:RevDrStrangelovehttps://www.reddit.com/r/LifeProTips/comments/369k76/lpt_request_automaticly_mute_laptop_after_headset/

答案3

对于 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

答案4

如果你有事件问题看到/etc/acpi/handler.sh我的回答。 这也是无设备代码作为Node 0x16

相关内容