bash 如何检测 Linux 中当前正在播放的声音?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17404443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect that the sound is currently playing in Linux?
提问by mhd
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:
我使用 ALSA。我想防止 PC 在播放声音时挂起。我使用这个 bash 代码来运行一个屏幕锁和一个挂起命令:
# 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:
我想检测播放的声音或视频并防止 PC 挂起。例如伪代码:
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?
如何使用命令行实用程序检测正在播放的声音或视频?
回答by CL.
Check if any /proc/asound/card*/pcm*/sub*/statusfile contains state: RUNNING.
检查任何/proc/asound/card*/pcm*/sub*/status文件是否包含state: RUNNING.
回答by glenn Hymanman
I use this:
我用这个:
pacmd list-sink-inputs
回答by Malvineous
If you use xscreensaverthen this command will tell you whether the screen is blanked:
如果您使用,xscreensaver则此命令将告诉您屏幕是否为空白:
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
回答by Peter MacDonald
You might try this script (requires installing xmacroplay):
你可以试试这个脚本(需要安装 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
回答by Peter MacDonald
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

