gpt4 book ai didi

python - 为python中的警报程序部分播放音频文件

转载 作者:太空宇宙 更新时间:2023-11-04 09:59:41 25 4
gpt4 key购买 nike

我在 python 中创建了一个警报,我想在警报时间到达后播放音轨的前 10 秒。

我尝试使用 subprocess.call,但我无法在 10 秒后终止代码,例如:

subprocess.call(["/usr/bin/vlc", "/home/panos/Desktop/U TUBE/SRW α3 - Battle God of Steel.webm"])

这会播放轨道,但我不知道如何在 10 秒后停止它,也不知道如何在闹钟打开时不显示实际的 VLC 播放器。我还有哪些其他选择?我可以用子流程做些什么吗?我在想 bash 有播放音频 10 秒的命令,所以我可以从 subprocess 调用它,但我对 bash 命令一无所知

最佳答案

如果你可以使用subprocess.check_output,它有你可以设置的timeout参数。

import subprocess

command = ["/usr/bin/vlc", "/home/panos/Desktop/U TUBE/SRW α3 - Battle God of Steel.webm"]

try:
x=subprocess.check_output(command, shell=True, timeout=5)
except subprocess.TimeoutExpired as exc:
print("Command timed out: {}".format(exc))

我已经使用 sleep 命令对其进行了测试,它按预期工作。

import subprocess

try:
x = subprocess.check_output(["sleep", "10"], timeout=5)
except subprocess.TimeoutExpired as exc:
print("Command timed out: {}".format(exc))

输出:

>>> python3 timeout.py 
Command timed out: Command '['sleep', '10']' timed out after 5 seconds

关于python - 为python中的警报程序部分播放音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57309529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com