How to detect that the sound is currently playing in Linux?
Asked Answered
V

5

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?

Vertebral answered 1/7, 2013 at 12:24 Comment(1)
See unix.stackexchange.com/questions/61337/…Polygamist
P
21

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

Protecting answered 1/7, 2013 at 12:52 Comment(2)
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.Protecting
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 anywayFeverous
S
12

I use this:

pacmd list-sink-inputs
Stoddard answered 1/7, 2013 at 13:18 Comment(0)
B
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
Bipropellant answered 5/8, 2017 at 10:38 Comment(0)
E
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
Eton answered 25/12, 2017 at 21:33 Comment(0)
E
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
Eton answered 29/12, 2017 at 16:26 Comment(1)
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.Kelle

© 2022 - 2024 — McMap. All rights reserved.