通过命令行调整音量,以使音量通知弹出。

有没有办法通过命令行调整系统音量,以便仍然显示默认音量弹出窗口(在笔记本上按媒体键时弹出的那个)。
我需要这个功能来控制我的遥控器。 它将使用一个lircrc文件和irexec来运行。

引用自https://bugs.launchpad.net/ubuntu/+source/gnome-settings-daemon/+bug/871133/comments/18;与https://askubuntu.com/q/10525/642296相关。 - cxw
5个回答

安装xdotool软件包,并尝试执行。
xdotool key XF86AudioLowerVolume

并且

xdotool key XF86AudioRaiseVolume

1你可能需要在前面加上DISPLAY=:0(或者其他值,如果你的显示器不同)以便lirc用户能够将其发送到正确的位置。但也可能不需要。 - Oli
1非常感谢!我不需要设置DISPLAY变量... - Lincoln
1在Ubuntu键盘快捷方式设置中,您可能需要在按键后使用--clearmodifiers参数来使用它。 - Pablo Bianchi
@Oli 是的,你需要它来例如通过SSH改变音量。 - wjandrea
根据我的经验,Unity很容易重新映射键,不需要--clearmodifiers,但是对于xbindkeys来说,你绝对需要它。 - wjandrea

你可以将一个快捷方式绑定到我找到的脚本上{{link1:在Arch论坛中}}(需要安装libnotify-bin软件包)。
#!/bin/sh

usage="usage: $0 -c {up|down|mute} [-i increment] [-m mixer]"
command=
increment=5%
mixer=Master

while getopts i:m:h o
do case "$o" in
    i) increment=$OPTARG;;
    m) mixer=$OPTARG;;
    h) echo "$usage"; exit 0;;
    ?) echo "$usage"; exit 0;;
esac
done

shift $(($OPTIND - 1))
command=$1

if [ "$command" = "" ]; then
    echo "usage: $0 {up|down|mute} [increment]"
    exit 0;
fi

display_volume=0

if [ "$command" = "up" ]; then
    display_volume=$(amixer set $mixer $increment+ unmute | grep -m 1 "%]" | cut -d "[" -f2|cut -d "%" -f1)
fi

if [ "$command" = "down" ]; then
    display_volume=$(amixer set $mixer $increment- unmute | grep -m 1 "%]" | cut -d "[" -f2|cut -d "%" -f1)
fi

icon_name=""

if [ "$command" = "mute" ]; then
    if amixer get Master | grep "\[on\]"; then
        display_volume=0
        icon_name="notification-audio-volume-muted"
        amixer set $mixer mute
    else
        display_volume=$(amixer set $mixer unmute | grep -m 1 "%]" | cut -d "[" -f2|cut -d "%" -f1)
    fi
fi

if [ "$icon_name" = "" ]; then
    if [ "$display_volume" = "0" ]; then
        icon_name="notification-audio-volume-off"
    elif [ "$display_volume" -lt "33" ]; then
        icon_name="notification-audio-volume-low"
    elif [ "$display_volume" -lt "67" ]; then
        icon_name="notification-audio-volume-medium"
    else
        icon_name="notification-audio-volume-high"
    fi
fi
notify-send " " -i $icon_name -h int:value:$display_volume -h string:synchronous:volume

在Ubuntu 10.10上似乎运行良好。

控制音量

您可以使用amixer来控制音量,例如:

amixer set 'Master' 50%
amixer set 'Master' 10%+
amixer set 'Master' 2dB-

您可能需要使用例如-c 1来设置声卡,以用于第二个声卡,请参阅man amixer

播放声音

可以使用类似aplaypaplay的播放器来播放声音,例如。

paplay /usr/share/sounds/freedesktop/stereo/audio-volume-change.oga

你可能想看一下这个问题:我在哪里找到系统声音? 显示屏幕通知
您可以使用X On-Screen Display库XOSD来重现屏幕上的通知。该软件包名为xosd-bin,命令osd_cat用于在屏幕上显示文本、状态栏等。
osd_cat -b percentage -P 20 -T Status: -f "-adobe-helvetica-bold-*-*--34-*-*-*-*"

显示

enter image description here

请参考这个德语维基页面以获取选项和示例,以及man osd_cat以获取更多信息。

这是脚本的改进版本htorque posted
在14.04上对我有效。如果在16.04或更新版本上有效,请告诉我。
它需要安装libnotify-bin
#!/bin/sh
# Adjust the volume, play a sound, and show a notification.
#
# Replacement for default Ubuntu volume adjustment behaviour.
#
# Based on https://askubuntu.com/a/12769/301745

command=""
device="pulse"
display_volume=0
icon_name="error"
increment=5
mixer="Master"
usage="usage: $0 [-d device] [-i increment] [-m mixer] (up|down|mute)"

# For compatibility with SSH sessions.
export DISPLAY=:0

_amixer(){
    # amixer alias
    local set_get="$1"
    shift
    amixer -D "$device" "$set_get" "$mixer" "$@"
}

_get_display_volume(){
    # grep alias
    grep -Pom 1 '(?<=\[)[0-9]+(?=%\])'
}

while getopts d:hi:m: opt; do
    case "$opt" in
        d)
            device="$OPTARG"
            ;;
        h)
            echo "$usage"
            exit 0
            ;;
        i)
            increment="$OPTARG"
            ;;
        m)
            mixer="$OPTARG"
            ;;
        ?)
            echo "$usage"
            exit 1
            ;;
    esac
done

shift "$(($OPTIND - 1))"
command="$1"

case "$command" in
    down)
        display_volume="$(
            _amixer set "$increment%-" unmute |
                _get_display_volume
            )"
        ;;
    mute)
        if _amixer get | grep -q "\[on\]"; then
            display_volume=0
            icon_name="notification-audio-volume-muted"
            _amixer set mute > /dev/null
        else
            display_volume="$(
                _amixer set unmute |
                    _get_display_volume
                )"
        fi
        ;;
    up)
        display_volume="$(
            _amixer set "$increment%+" unmute |
                _get_display_volume
            )"
        ;;
    *)
        echo "$usage"
        exit 1
        ;;
esac

if [ "$icon_name" = "error" ]; then
    if [ "$display_volume" = "0" ]; then
        icon_name="notification-audio-volume-off"
    elif [ "$display_volume" -lt "33" ]; then
        icon_name="notification-audio-volume-low"
    elif [ "$display_volume" -lt "67" ]; then
        icon_name="notification-audio-volume-medium"
    else
        icon_name="notification-audio-volume-high"
    fi

    # In a subshell in the background to minimize latency.
    ( canberra-gtk-play --id=audio-volume-change & )
fi

notify-send "Volume: $display_volume%" -i "$icon_name" -h "string:synchronous:volume" -h "int:value:$display_volume"

我安装了xmacro并在.lircrc文件中添加了以下行:
begin
        prog = irexec
        button = KEY_VOLUMEUP
        repeat = 1
        delay = 2
        config = echo KeyStrPress XF86AudioRaiseVolume KeyStrRelease XF86AudioRaiseVolume | xmacroplay $DISPLAY
end
begin
        prog = irexec
        button = KEY_VOLUMEDOWN
        repeat = 1
        delay = 2
        config = echo KeyStrPress XF86AudioLowerVolume KeyStrRelease XF86AudioLowerVolume | xmacroplay $DISPLAY
end
begin
        prog = irexec
        button = KEY_MUTE
        config = echo KeyStrPress XF86AudioMute KeyStrRelease XF86AudioMute | xmacroplay $DISPLAY
end