21

I use ALSA. I want to prevent PC to suspend while a sound is played. I use this bash code to run a screen locker and a suspend command:

# Run a screen locker.
#xautolock -time 5 -locker slimlock &

# Run suspend
#xautolock -time 6 -locker 'systemctl suspend' &

I want to detect that sound or video is played and prevent PC to suspend. For instance a pseudocode:

if (video is not played)
{
     run a screen locker
}

if (sound is not played and video is not played)
{
     run suspend command
}

How to detect that a sound or a video is playing with a command line utility?

mhd
  • 535
  • 1
  • 5
  • 12
  • See http://unix.stackexchange.com/questions/61337/testing-from-a-script-if-audio-devices-are-in-silent – devnull Jul 01 '13 at 12:57

5 Answers5

21

Check if any /proc/asound/card*/pcm*/sub*/status file contains state: RUNNING.

CL.
  • 173,858
  • 17
  • 217
  • 259
  • 1
    In the general case, video output cannot be distinguished from any other graphics output. Try using `ps` to see whether your video player application is running. – CL. Jul 01 '13 at 16:41
  • 1
    This may not be a useful comment, but I thought about detecting video usage for a little while, then I realised that there is generally audio output when a video is playing, therefore you don't really need to detect video usage. This was useful for me anyway – AlexMorley-Finch Dec 18 '15 at 20:59
12

I use this:

pacmd list-sink-inputs
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
1

If you use xscreensaver then this command will tell you whether the screen is blanked:

xscreensaver-command -time | grep -q 'screen \(locked\|blanked\)'

Typically video players will stop the screensaver from activating, so if it has activated then it probably means there is no video playing.

You can use it like this:

if xscreensaver-command -time | grep -q 'screen \(locked\|blanked\)'; then
    echo Screen is off (screensaver active)
else
    echo Screen is on, video might be playing
fi
Malvineous
  • 25,144
  • 16
  • 116
  • 151
1

You might try this script (requires installing xmacroplay):

#!/bin/bash
# Script to prevent screen blanking when audio is playing.
command -v xmacroplay > /dev/null 2>&1 || { echo "ERROR: must install xmacroplay"; exit -1; }
while true; do
    sleep 50
    if pacmd list-sink-inputs  | grep -w state | grep -q RUNNING ; then
       xmacroplay :0 >& /dev/null <<EOF
MotionNotify 90 90
MotionNotify 120 120
EOF
    fi
done
1

While the other script works, it keeps moving the mouse to a fixed location. This one tries to move it only if it hasn't changed, and keeps it near the current location.

#!/bin/bash
# Script to prevent screen blanking when audio is playing.
if [ -z DISPLAY ]; then
   DISPLAY=:0
fi
while true; do
    sleep 50
    if pacmd list-sink-inputs  | grep -w state | grep -q RUNNING ; then
        xdotool mousemove_relative -- -1 -1
        sleep 1
        xdotool mousemove_relative -- 1 1 
    fi
done
  • Just wanted to add there's a similar solution for sway, which I use: `if pactl list | grep -q RUNNING; then swaymsg 'seat - cursor move -1 0'; swaymsg 'seat - cursor move +1 0'; fi`. Inlined it here but in my setup the `swaymsg` commands reside in a `function inhibit`, plus I have a more flexible structure for checking other stuff besides pulseaudio. Also, if you use something other than pulse (Pipewire, pure-alsa, Jack etc) it will be necessary to adjust the test accordingly. p.s.: The previous `> /dev/null`, replaced with `-q` to match the post. – Fernando Cordeiro Jun 24 '22 at 21:16