18

我使用 ALSA。我想防止 PC 在播放声音时挂起。我使用这个 bash 代码来运行屏幕锁和挂起命令:

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

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

我想检测是否播放了声音或视频并阻止 PC 挂起。例如一个伪代码:

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

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

如何使用命令行实用程序检测声音或视频正在播放?

4

5 回答 5

19

检查是否有任何/proc/asound/card*/pcm*/sub*/status文件包含state: RUNNING.

于 2013-07-01T12:52:54.273 回答
9

我用这个:

pacmd list-sink-inputs
于 2013-07-01T13:18:45.590 回答
1

你可以试试这个脚本(需要安装 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
于 2017-12-25T21:33:30.803 回答
1

当另一个脚本工作时,它会不断将鼠标移动到固定位置。这个只有在它没有改变的情况下才会尝试移动它,并将它保持在当前位置附近。

#!/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
于 2017-12-29T16:26:35.877 回答
1

如果您使用xscreensaver,则此命令将告诉您屏幕是否为空白:

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

通常,视频播放器会阻止屏幕保护程序激活,因此如果它已激活,则可能意味着没有视频播放。

你可以像这样使用它:

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
于 2017-08-05T10:38:51.327 回答