Python 音频播放程序 - 随机播放并设置定时停止
import os
import random
import time
import datetime
from audio_play import AudioPlay
# 定义唤醒词和音频文件目录
base_dir_wakeup = "D:\python-jiaoben\performance\wakeup\"
base_dir_order = "D:\python-jiaoben\performance\order\"
# 设置开始时间
start_time = datetime.datetime.now()
while True:
# 初始化音频播放对象
audio_play = AudioPlay()
# 获取音频输入输出设备信息
audio_play.get_in_out_devices()
# 播放唤醒词
audio_play.run(base_dir_wakeup + '唤醒词.wav', name='test', sleep=0)
# 从在线文件夹中随机选择一个文件进行播放
path = "D:\python-jiaoben\performance\online\"
str_list = os.listdir(path)
index = random.randint(0, len(str_list) - 1)
print(str_list[index])
audio_play.run(base_dir_order + str_list[index], name='test', sleep=3)
# 播放完毕后等待一段时间再停止播放
time.sleep(15)
audio_play.terminate_run()
# 随机等待一段时间再次播放
sleep_time = random.randint(10, 30)
print(sleep_time, datetime.datetime.now())
time.sleep(sleep_time)
# 统计运行时间,达到12小时后停止程序
run_time = datetime.datetime.now()
if (run_time - start_time).total_seconds() >= 43200:
break
原文地址: https://www.cveoy.top/t/topic/nB4n 著作权归作者所有。请勿转载和采集!