Linux Mint: Como evitar a suspensão enquanto a música está tocando

5

Eu tenho um problema simples: gostaria que meu computador evitasse a suspensão quando a música está tocando em um aplicativo de desktop no meu computador - quero fazer melhor do que um script que mantém meu computador acordado sempre que um aplicativo de música estiver aberto . Estou executando o Linux Mint 18.

Eu imagino que poderia funcionar aplicativo por aplicativo, mas eu também me pergunto se há uma maneira de fazer isso, aproveitando o applet de som Cinnamon normal (ou similar). Esse applet é capaz de detectar e controlar a reprodução de qualquer aplicativo de música que o usuário tenha aberto - Spotify, Clementine, Banshee ou outros. Existe alguma maneira de um script determinar, a partir do applet de som ou de um aplicativo específico, se a música está tocando?

Estou ciente desta questão: de suspender enquanto toca música no Spotify , mas a resposta aceita não resolve o problema, e a cafeína mudou completamente desde então.

Meu comportamento desejado é benéfico por vários motivos:

  • Se o usuário deixar um aplicativo de música aberto em segundo plano, mas com a reprodução interrompida, o computador não ficará acordado indefinidamente.
  • Se o usuário deixar o computador com a reprodução de músicas, pretendendo que a música continue, eles não precisarão se preocupar em ter seu reprodutor de música em tela cheia para manter a música ativa.
  • O usuário pode aproveitar esse comportamento de maneiras diferentes. Por exemplo, eles poderiam selecionar 20 minutos de música relaxante antes de dormir. Quando a lista de reprodução de 20 minutos terminar, o computador será suspenso durante a noite. A duração do sono se ajusta automaticamente ao tamanho da lista de reprodução.

Estou disposto a eliminar isso, mas nunca escrevi um roteiro para algo dessa natureza e não sei por onde começar. Isso parece tão básico, mas útil, que talvez devesse ser um pedido de recurso para o Mint.

    
por mb7744 27.01.2017 / 23:21

2 respostas

1

Eu tenho lutado com a mesma coisa. Eu encontrei alguns outros posts que falaram sobre o uso de pactl para verificar o status do pulseaudio. Aqui está um link:

link

Eu tentei criar um script muito simples que irá alterar o tempo limite de suspensão nas configurações de energia do Cinnamon para nunca temporariamente enquanto a música estiver tocando e, em seguida, reverter quando a música parar. Esta é a minha primeira tentativa real em um script, então se você puder melhorar, fique à vontade. Provavelmente só funcionará em Canela.

#!/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
    
por 03.08.2018 / 16:03
1

Eu fiz um pouco mais de trabalho no script acima e agora ele deve funcionar no Cinnamon, Mate e Ubuntu executando o Gnome. Eu experimentei as versões Live USB do Linux Mint 19 Cinnamon e Mate, assim como o Ubuntu 18.04 - Gnome. Espero que isso seja útil para algumas outras pessoas.

Jay

#!/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

Ou aqui está uma versão que usa o ALSA em vez do 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
    
por 08.08.2018 / 04:45