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

mhd picture mhd · Jul 1, 2013 · Viewed 9.3k times · Source

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?

Answer

CL. picture CL. · Jul 1, 2013

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