音频仅在尝试使用 pygame 在 python 中杀死程序时工作
python 197
原文标题 :Audio working only when trying to kill program in python using pygame
我只是为了好玩而制作了一个程序,但音频不起作用,当我试图终止程序时,音频突然开始工作,当我按下取消时,它又停止工作了。我这样做了几次,并且仅在尝试杀死程序时才发现音频有效,但是为什么?这是代码
import time
current_time = time.localtime()
hour = current_time.tm_hour
minute = current_time.tm_min
if (hour>5):
x=1
while x==1 :
from pygame import *
print("TIME TO WAKE UP!!!")
mixer.init()
mixer.music.load('Air-raid-siren.ogg')
mixer.music.play()
回复
我来回复-
The Myth 评论
您的代码似乎不合适。首先,您正在检查小时是否 > 5,然后为 while 循环定义一个不需要的变量 x。这是你应该做的:
import time from pygame import mixer current_time = time.localtime() hour = current_time.tm_hour minute = current_time.tm_min while hour>5: print("TIME TO WAKE UP!!!") mixer.init() mixer.music.load(location_of_music) mixer.music.play() hour=5
我所做的基本上是消除你的 if 语句和变量 x。我已经定义了 while > hour 5 并且在最后一行下面我已经设置了 hour = 5。这是因为如果没有你的代码将永远运行并且它只是一个系统延迟,你在杀死时获得输出程序。实际上,输出是存在的,但它是一个很大的垃圾邮件,您的设备无法显示它。这就是为什么我建议使用增量或完全删除 while 循环,因为不需要它(你不想要垃圾邮件)。我什至认为播放音乐文件不需要 pygame。访问此链接以更好地了解在 pyhton 中播放声音
2年前