import datetime import os import time import pygame from pygame import mixer from pygame.locals import USEREVENT STOPEVENT = USEREVENT + 1 #音乐停止事件 h = 9 # 时间 m = 26 # 分钟 play_long = 60 * 10 # 播放时长 def play(start): paths = os.listdir(‘.‘) #当前目录下所欲文件 def ismp3(path): return path.endswith(".mp3") paths = list(filter(ismp3, paths)) #保留mp3文件 if len(paths) > 0: cur = 0 mixer.init() pygame.init() mixer.music.set_endevent(STOPEVENT) #设置音乐停止事件 mixer.music.load(paths[cur]) #加载 mixer.music.play() $播放 #当超过播放时长后,停止播放 while (datetime.datetime.now() - start).seconds < play_long: time.sleep(1) #1秒监听一次事件 event = pygame.event.poll() if event.type == STOPEVENT: cur += 1 if cur != len(paths): mixer.music.load(paths[cur]) mixer.music.play() else: break; pygame.quit() def detect(): while True: now = datetime.datetime.now() if now.hour == h and now.minute == m: play(now) else: time.sleep(1) if __name__ == ‘__main__‘: detect()
原文地址:https://www.cnblogs.com/dreamycloud/p/8438540.html
时间: 2024-10-08 11:59:55