如何检测Linux当前播放的声音?

时间:2013-07-01 12:24:18

标签: bash audio video alsa

我使用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
}

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

5 个答案:

答案 0 :(得分:12)

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

答案 1 :(得分:5)

我用这个:

pacmd list-sink-inputs

答案 2 :(得分: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

答案 3 :(得分:0)

您可以尝试使用此脚本(需要安装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

答案 4 :(得分:0)

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

cd $WORKSPACE

java -cp "lib/*:lib/selenium/*:lib/POI/*:bin:" org.testng.TestNG $WORKSPACE/Smoke_OrderIDCards.xml