python - 如何在 Python 2.7 中根据当前时间播放随机音频文件?
问题描述
背景:我使用 Raspberry Pi rev 2 B 运行自然声音白噪声发生器,它会根据晚上/早上的时间随机播放不同长度的音轨。有些曲目只有一分钟,有些则长达数小时。我正在寻找一种方法来检查时间并根据时间更改播放的声音类型。
当前问题:我可以在程序第一次执行时启动适当的音频,但是一旦omxplayer启动,timeloop执行就会停止轮询。
我试图在不中断确定播放哪种音频的时间检查器的情况下调用 OMXPlayer,但是一旦音频播放开始,我就无法继续检查时间。即使play_audio()
函数不是递归的,我仍然希望时间检查器在音频播放时继续执行
#!/usr/bin/env python
import datetime, time, os, subprocess, random
from timeloop import Timeloop
from datetime import timedelta
from time import sleep
from omxplayer.player import OMXPlayer
from pathlib import Path
tl = Timeloop()
running_cycle = "off" # default value for the time cycle currently running
#function to check current time cycle
def check_time () :
dt_now = datetime.datetime.now()
t_now = dt_now.time()
t_night = datetime.time(hour=2,minute=0)
t_twilight = datetime.time(hour=4,minute=45)
t_morning = datetime.time(hour=7,minute=0)
t_end = datetime.time(hour=10,minute=0)
if t_night <= t_now < t_twilight:
return "night"
elif t_twilight <= t_now < t_morning:
return "twilight"
elif t_morning <= t_now < t_end:
return "morning"
else:
return "off"
# function that plays the audio
def play_audio (time_cycle):
subprocess.call ("killall omxplayer", shell=True)
randomfile = random.choice(os.listdir("/home/pi/music/nature-sounds/" + time_cycle))
file = '/home/pi/music/nature-sounds/' + time_cycle + '/' + randomfile
path = Path(file)
player = OMXPlayer(path)
play_audio (time_cycle)
# function that determines whether to maintain current audio cycle or play another
def stay_or_change():
global running_cycle
current_cycle = check_time()
if running_cycle != current_cycle:
if current_cycle == "off" :
player.quit()
else:
running_cycle = current_cycle
print "Now playing: " + running_cycle + " @{}".format(time.ctime())
play_audio(running_cycle)
#starts timeloop checker to play audio - works until stay_or_change() calls play_audio
@tl.job(interval=timedelta(seconds=10))
def job_10s():
print "10s job - running cycle: " + running_cycle + " - current time: {}".format(time.ctime())
stay_or_change()
# starts the timeloop
if __name__ == "__main__":
tl.start(block=True)
我也尝试过运行 OMXPlayer,subprocess.run()
但播放器启动后它似乎仍然挂断。对于后台线程媒体播放器、进程守护进程或基于时间的执行方法,我完全愿意接受任何建议。
我是 Python 新手。
解决方案
我的递归全错了,所以它陷入了无限循环,而 timeloop 函数对于这个解决方案来说并不是真正可行的。相反,我有一个播放声音的函数,然后调用该函数检查时间并从适当的子目录播放(或不播放任何内容并等待)。
这是我设法想出的:
#!/usr/bin/env python
import datetime, time, os, subprocess, random
from datetime import timedelta
from time import sleep
from omxplayer.player import OMXPlayer
def check_time () :
dt_now = datetime.datetime.now()
t_now = dt_now.time()
t_night = datetime.time(hour=0,minute=0)
t_twilight = datetime.time(hour=5,minute=45)
t_morning = datetime.time(hour=7,minute=45)
t_end = datetime.time(hour=10,minute=0)
if t_night <= t_now < t_twilight:
return "night"
elif t_twilight <= t_now < t_morning:
return "twilight"
elif t_morning <= t_now < t_end:
return "morning"
else:
return "off"
def play_audio (time_cycle):
randomfile = random.choice(os.listdir("/home/pi/music/nature-sounds/" + time_cycle))
file = '/home/pi/music/nature-sounds/' + time_cycle + '/' + randomfile
print "playing track: " + randomfile
cmd = 'omxplayer --vol -200 ' + file
subprocess.call (cmd, shell=True)
what_to_play()
def what_to_play():
current_cycle = check_time()
if current_cycle == "off" :
print "sounds currently off - @{}".format(time.ctime())
time.sleep(30)
what_to_play()
else:
print "Now playing from " + current_cycle + " @{}".format(time.ctime())
play_audio(current_cycle)
what_to_play()
推荐阅读
- azure - Azure 函数 - local.settings.json 与 launchSettings.json
- serenity-platform - 我在 Endpoint 中收到 system.string 值错误?
- php - PHPMailer 接受原始文件名作为附件,但不作为变量
- r - 嵌套类实例 (OOP) 上的 R 方法
- swift - Firestore - 如何将多个字段查询到单个数组
- angular - 在 Angular 中手动重新渲染 FusionChart
- javascript - 尽管我的 firebase 代码 100% 正确(反应),但我的状态仍然为空
- android - Android TextInputLayout PasswordToggle 不工作
- javascript - 我的产品数组没有使用产品列表组件在 Angular 上呈现
- node.js - 将 Node.js 与图形 API 一起使用时,我似乎无法将 @odata.id 添加到对象