Linux Mint:如何避免在播放音乐时暂停

Linux Mint:如何避免在播放音乐时暂停

我有一个简单的问题:我希望我的计算机在播放音乐时避免暂停在我计算机上的桌面应用程序上——我想要做得比脚本更好,只要音乐应用程序启动,我的计算机就保持唤醒状态打开。我正在运行 Linux Mint 18。

我想它可以逐个应用程序工作,但我也想知道是否有办法通过利用通常的 Cinnamon 声音小程序(或类似的)来做到这一点。该小程序能够检测和控制用户打开的任何音乐应用程序的播放——Spotify、Clementine、Banshee 或其他应用程序。脚本是否可以通过某种方式从声音小程序或特定应用程序确定音乐是否正在播放?

我知道这个问题:防止 Mint 在 Spotify 中播放音乐时暂停但接受的答案并没有解决问题,咖啡因从那时起就完全改变了。

我期望的行为是有益的,原因如下:

  • 如果用户在后台打开音乐应用程序,但播放停止,计算机将不会无限期地保持唤醒状态。
  • 如果用户离开计算机时播放音乐,并希望继续播放音乐,则他们无需担心音乐播放器处于全屏状态以保持音乐继续播放。
  • 用户可以通过不同的方式利用此行为。例如,他们可以在睡前选择 20 分钟的轻松音乐。当 20 分钟的播放列表结束时,计算机将在夜间暂停。因此,睡眠持续时间会自动调整为播放列表的长度。

我愿意解决这个问题,但我从来没有为这种性质的东西编写过脚本,并且不知道从哪里开始。这感觉很基本但很有用,也许它应该是 Mint 的一个功能请求。

答案1

我一直在为同样的事情而苦苦挣扎。我发现了一些其他帖子讨论使用 pactl 检查pulseaudio 的状态。这是一个链接:

https://superuser.com/questions/393448/detecting-audio-playing-in-a-bash-script

我试图编写一个非常简单的脚本,它将在音乐播放时将 Cinnamon 电源设置中的暂停超时更改为从不暂时,然后在音乐停止时恢复。这是我第一次真正尝试编写脚本,因此如果您可以对其进行改进,请随意。但它可能只适用于肉桂。

#!/bin/sh

# Script to temporarily set Cinnamon's suspend timout for AC and battery to "Never"
# while audio is playing.  It then reverts the settings when audio is no longer detected.

# Create directory to store settings if it doesn't exist.
if [ ! -d ~/.config/audiocaffeine ]; then
    echo "Configuration directory not found!"
    echo "Creating ~/.config/audiocaffeine"
    mkdir ~/.config/audiocaffeine
fi

# Restore previous value for AC suspend timeout if script
# was interrupted.
if [ -f ~/.config/audiocaffeine/acsuspend ]; then
    echo "Restoring previous AC suspend timeout."
    read acsuspendtime < ~/.config/audiocaffeine/acsuspend
    gsettings set org.cinnamon.settings-daemon.plugins.power sleep-inactive-ac-timeout $acsuspendtime
    echo "Removing temporary file ~/.config/audiocaffeine/acsuspend"
    rm ~/.config/audiocaffeine/acsuspend
fi

# Restore previous value for battery suspend timeout if script
# was interrupted.
if [ -f ~/.config/audiocaffeine/battsuspend ]; then
    echo "Restoring previous battery suspend timeout."
    read battsuspendtime < ~/.config/audiocaffeine/battsuspend
    gsettings set org.cinnamon.settings-daemon.plugins.power sleep-inactive-battery-timeout $battsuspendtime
    echo "Removing temporary file ~/.config/audiocaffeine/battsuspend"
    rm ~/.config/audiocaffeine/battsuspend
fi

# Start main loop to check if audio is playing

while true; do

    # Use pactl to detect if there are any running audio sources.
    if pactl list | grep -q RUNNING; then

        echo "Audio detected."

        # If AC timeout was not previously saved, then save it.
        if [ ! -f ~/.config/audiocaffeine/acsuspend ]; then
            echo "Saving current AC suspend timeout."
            gsettings get org.cinnamon.settings-daemon.plugins.power sleep-inactive-ac-timeout > ~/.config/audiocaffeine/acsuspend
        fi

        # If battery timeout was not previously saved, then save it.
        if [ ! -f ~/.config/audiocaffeine/battsuspend ]; then
            echo "Saving current battery suspend timeout."
            gsettings get org.cinnamon.settings-daemon.plugins.power sleep-inactive-battery-timeout > ~/.config/audiocaffeine/battsuspend
        fi

        # Set the suspend timouts to Never using gsettings.
        echo "Changing suspend timeouts."
        gsettings set org.cinnamon.settings-daemon.plugins.power sleep-inactive-ac-timeout 0
        gsettings set org.cinnamon.settings-daemon.plugins.power sleep-inactive-battery-timeout 0

    else
        echo "No audio detected."

        # Restore previous value for AC suspend timeout and delete the
        # temporary file storing it.
        if [ -f ~/.config/audiocaffeine/acsuspend ]; then
            echo "Restoring previous AC suspend timeout."
            read acsuspendtime < ~/.config/audiocaffeine/acsuspend
            gsettings set org.cinnamon.settings-daemon.plugins.power sleep-inactive-ac-timeout $acsuspendtime
            echo "Removing temporary file ~/.config/audiocaffeine/acsuspend"
            rm ~/.config/audiocaffeine/acsuspend
        fi

        # Restore previous value for battery suspend timeout and delete the
        # temporary file storing it.
        if [ -f ~/.config/audiocaffeine/battsuspend ]; then
            echo "Restoring previous battery suspend timeout."
            read battsuspendtime < ~/.config/audiocaffeine/battsuspend
            gsettings set org.cinnamon.settings-daemon.plugins.power sleep-inactive-battery-timeout $battsuspendtime
            echo "Removing temporary file ~/.config/audiocaffeine/battsuspend"
            rm ~/.config/audiocaffeine/battsuspend
        fi

    fi

    # Pause the script for 60 seconds before doing the loop again.
    sleep 60s

done

答案2

我对上面的脚本做了更多的工作,它现在应该可以在运行 Gnome 的 Cinnamon、Mate 和 Ubuntu 中运行。我已经在 Linux Mint 19 Cinnamon 和 Mate 的 Live USB 版本以及 Ubuntu 18.04 - Gnome 上进行了尝试。我希望这对其他人有帮助。

杰伊

#!/bin/sh

# Audiocaffeine - Pulseaudio

# Works in Linux Mint 19 - Cinnamon, Linux Mint 19 - Mate, Ubuntu 18.04 - Gnome

# Script to temporarily set suspend timout for AC and battery to "Never"
# while audio is playing.  It then reverts the settings when audio is no longer detected.

# Determine if a valid desktop environment is running and exit if it doesn't.
echo "Reported desktop environment: ""$XDG_CURRENT_DESKTOP"
if [ "$XDG_CURRENT_DESKTOP" = "X-Cinnamon" ]; then
    actimeoutid="org.cinnamon.settings-daemon.plugins.power sleep-inactive-ac-timeout"
    batttimeoutid="org.cinnamon.settings-daemon.plugins.power sleep-inactive-battery-timeout"
    disablevalue=0
elif [ "$XDG_CURRENT_DESKTOP" = "MATE" ]; then
    actimeoutid="org.mate.power-manager sleep-computer-ac"
    batttimeoutid="org.mate.power-manager sleep-computer-battery"
    disablevalue=0
elif [ "$XDG_CURRENT_DESKTOP" = "ubuntu:GNOME" ]; then
    actimeoutid="org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type"
    batttimeoutid="org.gnome.settings-daemon.plugins.power sleep-inactive-battery-type"
    disablevalue="nothing"
else
    echo "No valid desktop environment is running"
    exit 1
fi

# Create .config directory to store settings if it doesn't exist.
if [ ! -d ~/.config ]; then
    echo ".config directory not found!"
    echo "Creating ~/.config"
    mkdir ~/.config
fi

# Create audiocaffeine directory to store settings if it doesn't exist.
if [ ! -d ~/.config/audiocaffeine ]; then
    echo "Configuration directory not found!"
    echo "Creating ~/.config/audiocaffeine"
    mkdir ~/.config/audiocaffeine
fi

# Restore previous value for AC suspend timeout if script
# was interrupted.
if [ -f ~/.config/audiocaffeine/acsuspend ]; then
    echo "Restoring previous AC suspend timeout."
    read acsuspendtime < ~/.config/audiocaffeine/acsuspend
    gsettings set $actimeoutid $acsuspendtime
    echo "Removing temporary file ~/.config/audiocaffeine/acsuspend"
    rm ~/.config/audiocaffeine/acsuspend
fi

# Restore previous value for battery suspend timeout if script
# was interrupted.
if [ -f ~/.config/audiocaffeine/battsuspend ]; then
    echo "Restoring previous battery suspend timeout."
    read battsuspendtime < ~/.config/audiocaffeine/battsuspend
    gsettings set $batttimeoutid $battsuspendtime
    echo "Removing temporary file ~/.config/audiocaffeine/battsuspend"
    rm ~/.config/audiocaffeine/battsuspend
fi

# Start main loop to check if audio is playing

while true; do

    # Use pactl to detect if there are any running audio sources.
    if pactl list | grep -q "State: RUNNING"; then

        echo "Audio detected."

        # If AC timeout was not previously saved, then save it.
        if [ ! -f ~/.config/audiocaffeine/acsuspend ]; then
            echo "Saving current AC suspend timeout."
            gsettings get $actimeoutid > ~/.config/audiocaffeine/acsuspend
        fi

        # If battery timeout was not previously saved, then save it.
        if [ ! -f ~/.config/audiocaffeine/battsuspend ]; then
            echo "Saving current battery suspend timeout."
            gsettings get $batttimeoutid > ~/.config/audiocaffeine/battsuspend
        fi

        # Set the suspend timouts to Never using gsettings.
        echo "Changing suspend timeouts."
        gsettings set $actimeoutid $disablevalue
        gsettings set $batttimeoutid $disablevalue

    else
        echo "No audio detected."

        # Restore previous value for AC suspend timeout and delete the
        # temporary file storing it.
        if [ -f ~/.config/audiocaffeine/acsuspend ]; then
            echo "Restoring previous AC suspend timeout."
            read acsuspendtime < ~/.config/audiocaffeine/acsuspend
            gsettings set $actimeoutid $acsuspendtime
            echo "Removing temporary file ~/.config/audiocaffeine/acsuspend"
            rm ~/.config/audiocaffeine/acsuspend
        fi

        # Restore previous value for battery suspend timeout and delete the
        # temporary file storing it.
        if [ -f ~/.config/audiocaffeine/battsuspend ]; then
            echo "Restoring previous battery suspend timeout."
            read battsuspendtime < ~/.config/audiocaffeine/battsuspend
            gsettings set $batttimeoutid $battsuspendtime
            echo "Removing temporary file ~/.config/audiocaffeine/battsuspend"
            rm ~/.config/audiocaffeine/battsuspend
        fi

    fi

    # Pause the script for 60 seconds before doing the loop again.
    sleep 60s

done

或者这是一个使用 ALSA 而不是 Pulseaudio 的版本:

#!/bin/sh

# Audiocaffeine - ALSA

# Works in Linux Mint 19 - Cinnamon, Linux Mint 19 - Mate, Ubuntu 18.04 - Gnome

# Script to temporarily set suspend timout for AC and battery to "Never"
# while audio is playing.  It then reverts the settings when audio is no longer detected.

# Determine if a valid desktop environment is running and exit if it doesn't.
echo "Reported desktop environment: ""$XDG_CURRENT_DESKTOP"
if [ "$XDG_CURRENT_DESKTOP" = "X-Cinnamon" ]; then
    actimeoutid="org.cinnamon.settings-daemon.plugins.power sleep-inactive-ac-timeout"
    batttimeoutid="org.cinnamon.settings-daemon.plugins.power sleep-inactive-battery-timeout"
    disablevalue=0
elif [ "$XDG_CURRENT_DESKTOP" = "MATE" ]; then
    actimeoutid="org.mate.power-manager sleep-computer-ac"
    batttimeoutid="org.mate.power-manager sleep-computer-battery"
    disablevalue=0
elif [ "$XDG_CURRENT_DESKTOP" = "ubuntu:GNOME" ]; then
    actimeoutid="org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type"
    batttimeoutid="org.gnome.settings-daemon.plugins.power sleep-inactive-battery-type"
    disablevalue="nothing"
else
    echo "No valid desktop environment is running"
    exit 1
fi

# Create .config directory to store settings if it doesn't exist.
if [ ! -d ~/.config ]; then
    echo ".config directory not found!"
    echo "Creating ~/.config"
    mkdir ~/.config
fi

# Create audiocaffeine directory to store settings if it doesn't exist.
if [ ! -d ~/.config/audiocaffeine ]; then
    echo "Configuration directory not found!"
    echo "Creating ~/.config/audiocaffeine"
    mkdir ~/.config/audiocaffeine
fi

# Restore previous value for AC suspend timeout if script
# was interrupted.
if [ -f ~/.config/audiocaffeine/acsuspend ]; then
    echo "Restoring previous AC suspend timeout."
    read acsuspendtime < ~/.config/audiocaffeine/acsuspend
    gsettings set $actimeoutid $acsuspendtime
    echo "Removing temporary file ~/.config/audiocaffeine/acsuspend"
    rm ~/.config/audiocaffeine/acsuspend
fi

# Restore previous value for battery suspend timeout if script
# was interrupted.
if [ -f ~/.config/audiocaffeine/battsuspend ]; then
    echo "Restoring previous battery suspend timeout."
    read battsuspendtime < ~/.config/audiocaffeine/battsuspend
    gsettings set $batttimeoutid $battsuspendtime
    echo "Removing temporary file ~/.config/audiocaffeine/battsuspend"
    rm ~/.config/audiocaffeine/battsuspend
fi

# Start main loop to check if audio is playing

while true; do

    # Use ALSA to detect if there are any running audio sources.
    if grep -q RUNNING /proc/asound/card*/*p/*/status 2>&1; then

        echo "Audio detected."

        # If AC timeout was not previously saved, then save it.
        if [ ! -f ~/.config/audiocaffeine/acsuspend ]; then
            echo "Saving current AC suspend timeout."
            gsettings get $actimeoutid > ~/.config/audiocaffeine/acsuspend
        fi

        # If battery timeout was not previously saved, then save it.
        if [ ! -f ~/.config/audiocaffeine/battsuspend ]; then
            echo "Saving current battery suspend timeout."
            gsettings get $batttimeoutid > ~/.config/audiocaffeine/battsuspend
        fi

        # Set the suspend timouts to Never using gsettings.
        echo "Changing suspend timeouts."
        gsettings set $actimeoutid $disablevalue
        gsettings set $batttimeoutid $disablevalue

    else
        echo "No audio detected."

        # Restore previous value for AC suspend timeout and delete the
        # temporary file storing it.
        if [ -f ~/.config/audiocaffeine/acsuspend ]; then
            echo "Restoring previous AC suspend timeout."
            read acsuspendtime < ~/.config/audiocaffeine/acsuspend
            gsettings set $actimeoutid $acsuspendtime
            echo "Removing temporary file ~/.config/audiocaffeine/acsuspend"
            rm ~/.config/audiocaffeine/acsuspend
        fi

        # Restore previous value for battery suspend timeout and delete the
        # temporary file storing it.
        if [ -f ~/.config/audiocaffeine/battsuspend ]; then
            echo "Restoring previous battery suspend timeout."
            read battsuspendtime < ~/.config/audiocaffeine/battsuspend
            gsettings set $batttimeoutid $battsuspendtime
            echo "Removing temporary file ~/.config/audiocaffeine/battsuspend"
            rm ~/.config/audiocaffeine/battsuspend
        fi

    fi

    # Pause the script for 60 seconds before doing the loop again.
    sleep 60s

done

答案3

从软件管理器下载并安装咖啡因应用程序。然后在桌面面板中安装 Caffeine Indicator。然后您可以在闲暇时选择激活或停用咖啡因。在 Linux Mint MATE 19.3 上运行良好。

相关内容