如何通过命令行进行静音操作?

如何通过命令行将声音系统静音?
6个回答

假设您正在使用ALSA驱动程序,请运行以下命令:
amixer set Master mute   
amixer set Master unmute

或者,你可以直接使用:
amixer set Master toggle

切换静音开关。

8对于我的系统(准确地说),这只有一个方向:关闭/静音。无论是切换还是取消静音,都不能重新播放音乐。你有什么想法为什么会这样? - nutty about natty
2查看此链接:http://askubuntu.com/questions/77522/command-to-unmute-and-maximize-volume - nutty about natty
4这个解决方案适用于纯Alsa。对于带有pulseaudio的Alsa,请使用Tim的答案中的命令。或者不要更改命令,而是按照这个答案中的说明配置Alsa。否则,@nutty about natty无法取消静音的问题将会出现。 - tanius
在Ubuntu Server 14.04上,“Master”作为一个简单的控制选项不可用。我使用了“Speaker”来代替它。我通过运行sudo amixer并找到与Simple mixer control 'Speaker',0匹配的输出行来进行替换。 - brycemcd

这对我有效,而其他方法却没有起作用:
amixer -q -D pulse sset Master toggle

这是从nutty about natty's comment链接到第一个答案的内容。

1我也刚刚检查过,它也适用于14.04。 - Tim
1搞定了 :) - 这里是 Kubuntu(Ubuntu 14.04.2 LTS)。谢谢。 - hakre
2当Alsa与pulseaudio一起使用时,需要使用-D pulse选项(由于问题标记为pulseaudio,这应该是被接受的答案)。有关此解决方案的更多详细信息,请参见这里这里在askubuntu上。 - tanius
注意:当耳机或耳塞插入时,@goric所接受的答案无法使用,请使用此方法。 - user423626
1或者,比"toggle"更明确的是,你可以使用amixer -q -D pulse sset Master muteamixer -q -D pulse sset Master unmute。在Ubuntu 16.04上效果非常好。 - CPBL
@中職棒確定,當我回答這個問題時,那對我沒有起作用。 - Tim
这对我也起作用,可以将蓝牙音箱静音,而默认的笔记本按钮对其没有影响。 - user47206

我在我的脚本中使用“pactl”。从man page中得知:

set-sink-mute SINK 1|0|toggle:设置指定音频输出设备(通过符号名称或数字索引标识)的静音状态

要静音:
pactl set-sink-mute @DEFAULT_SINK@ true

取消静音:
pactl set-sink-mute @DEFAULT_SINK@ false

切换:

pactl set-sink-mute @DEFAULT_SINK@ toggle

使用0而不是@DEFAULT_SINK@来设置索引为0的音频输出设备。true等于"1",false等于"0"。
在Ubuntu 12.10上进行了测试。 在我的设置中,有时候amixer解除静音会因为某种原因失败。

1仍然适用于Ubuntu 15.10。 - tanius
很可能,这是在现代Ubuntu版本上执行操作的适当方式。在16.04上有效(无法使用amixer)。 - Marcus
2您还可以从静音切换到非静音,然后再切换回来:pactl set-sink-mute 0 toggle - Matthias Braun
1可能0号音频输出设备并不是相关的。对我来说,0号是显示器的输出,而1号是笔记本电脑的扬声器,所以我需要执行pactl set-sink-mute 1 0来取消静音它们。 - nnnmmm

在终端上输入以下内容以静音

amixer set Master mute

类型

amixer set Master unmute

测试于我使用的Ubuntu 10.10。

如果你正在使用`alsa`,请按照Goric的回答进行操作。
PulseAudio更好,但并不是那么简单:`pactl set-sink-mute 0 1`可以在第一个设备上工作,但如果你正在使用另一个输出设备的耳机,则无法生效。
更好的方法是使用`pactl info`来检查并获取要使用的默认输出设备。
DEFAULT_SINK=$(pactl info | grep "Default Sink" | cut -d " " -f3)

然后静音:
pactl set-sink-mute "$DEFAULT_SINK" "1"

取消静音:

pactl set-sink-mute "$DEFAULT_SINK" "0"

我写了一个脚本来管理我的笔记本上的pulseaudio。如果你想使用,将它保存为volume,提供执行权限chmod +x volume并将其添加到你的路径中ln -sv $PWD/volume /usr/local/bin/。以下是我的脚本:
#!/bin/bash
# script name: volume
# Author: glaudistong at gmail.com
# depends on: yad, coreutils, pulseaudio

ps -ef | grep "yad" | grep -E "Volume [^+\-]" | tr -s " " | cut -d " " -f2 | xargs -i kill "{}" 2>/dev/null
DEFAULT_SINK=$(pactl info | grep "Default Sink" | cut -d " " -f3)
DEFAULT_SOURCE=$(pactl info | grep "Default Source" | cut -d " " -f3)
case "$1" in 
    init)
    {
        ps -fe | grep yad | grep -q volume ||
        {
         yad --notification --command "volume up" --text "+ Volume +" --image ~/Pictures/volume-up-dark.png &
         yad --notification --command "volume down" --text "- Volume -" --image ~/Pictures/volume-down-dark.png &
        }
    };;
    up)
    {
        pactl set-sink-volume "$DEFAULT_SINK" +5%
        P=$(pactl list | grep -E "Name: $DEFAULT_SINK$|Volume" | grep "Name:" -A1 | tail -1 | cut -d% -f1 | cut -d/ -f2 | tr -d " ")
        iconl="$(echo -ne "\U1F50A")"
        iconr="$(echo -ne "\U1F56A")"
        timeout .6 yad --progress --percentage "$P" --timeout 1 --no-buttons --undecorated --text="$iconl Volume $P% $iconr" --no-focus --center --skip-taskbar --on-top &
    };;
    down)
    {
        pactl set-sink-volume "$DEFAULT_SINK" -5%
        P=$(pactl list | grep -E "Name: $DEFAULT_SINK$|Volume" | grep "Name:" -A1 | tail -1 | cut -d% -f1 | cut -d/ -f2 | tr -d " ")
        iconl="$(echo -ne "\U1F509")"
        iconr="$(echo -ne "\U1F569")"
        timeout .6 yad --progress --percentage "$P" --timeout 1 --no-buttons --undecorated --text="$iconl Volume $P% $iconr" --no-focus --center --skip-taskbar --on-top &
    };;
    mute)
    {
        ismute=$(pactl list | grep -E "Name: $DEFAULT_SINK$|Mute" | grep "Name:" -A1 | tail -1 |cut -d: -f2| tr -d " ")
        if [ "$ismute" == no ]; then
            s=1
            P=0
            icon="$(echo -ne "\U1F507")"
        else
            P=$(pactl list | grep -E "Name: $DEFAULT_SINK$|Volume" | grep "Name:" -A1 | tail -1 | cut -d% -f1 | cut -d/ -f2 | tr -d " ")
            icon=""
            s=0
        fi
        pactl set-sink-mute "$DEFAULT_SINK" "$s"
        echo $s > /sys/devices/platform/thinkpad_acpi/leds/platform::mute/brightness
        timeout .6 yad --progress --percentage "$P" --timeout 1 --no-buttons --undecorated --text="$icon Volume $P%" --no-focus --center --skip-taskbar --on-top &
    };;
    mic-up)
    {
        pactl set-source-volume "$DEFAULT_SOURCE" +5%
        P=$(pactl list | grep -E "Name: $DEFAULT_SOURCE$|Volume" | grep "Name:" -A1 | tail -1 | cut -d% -f1 | cut -d/ -f2 | tr -d " ")
        icon="$(echo -en "\U1F3A4")"
        timeout .6 yad --progress --percentage "$P" --timeout 1 --no-buttons --undecorated --text="$icon Volume Mic $P%" --no-focus --center --skip-taskbar --on-top &
    };;
    mic-down)
    {
        pactl set-source-volume "$DEFAULT_SOURCE" -5%
        icon="$(echo -en "\U1F3A4")"
        P=$(pactl list | grep -E "Name: $DEFAULT_SOURCE$|Volume" | grep "Name:" -A1 | tail -1 | cut -d% -f1 | cut -d/ -f2 | tr -d " ")
        timeout .6 yad --progress --percentage "$P" --timeout 1 --no-buttons --undecorated --text="$icon Volume Mic $P%" --no-focus --center --skip-taskbar --on-top &
    };;
    mic-mute)
    {
        ismute=$(pactl list | grep -E "Name: $DEFAULT_SOURCE$|Mute" | grep "Name:" -A1 | tail -1 |cut -d: -f2| tr -d " ")
        if [ "$ismute" == no ]; then
            s=1
            P=0
            icon="$(echo -en "\U1F507\U1F3A4")"
        else
            P=$(pactl list | grep -E "Name: $DEFAULT_SOURCE$|Volume" | grep "Name:" -A1 | tail -1 | cut -d% -f1 | cut -d/ -f2 | tr -d " ")
            s=0
            icon="$(echo -en "\U1F3A4")"
        fi
        pactl set-source-mute "$DEFAULT_SOURCE" "$s"
        echo $s > /sys/devices/platform/thinkpad_acpi/leds/platform::micmute/brightness
        timeout .6 yad --progress --percentage "$P" --timeout 1 --no-buttons --undecorated --text="$icon Volume Mic $P%" --no-focus --center --skip-taskbar --on-top &
    };;
    *)
        echo invalid option;;
esac;

1作为一个小的改进,你可以分别使用值为@DEFAULT_SINK@@DEFAULT_SOURCE@的默认接收器/发送器来引用,而不是对查询结果进行grep操作。 - ForeverZer0

如果您正在使用PulseAudio作为音频服务器,请执行以下操作。
pactl -- set-sink-mute @DEFAULT_SINK@ toggle # Also true/false to mute/unmute

静音和取消静音

如果使用alsa,请使用以下内容

amixer sset 'Master' toggle

静音和取消静音