嗨我需要同时使用我的警报声运行我的gui并在第二个对话框中单击确定按钮时停止迭代警报声。为了完成这个任务,我创建了2个文件,这是主文件(gui使用easygui)和AudioFile类使用pyaudio来播放和停止警报声。
主文件:
from easygui import *
import sys
from AudioFile import *
predictions[0] = 1
a = AudioFile("alarm.wav")
if (predictions[0] == 1):
while 1:
#play alarm sound
a.play()
msgbox("Critical Situation Detected!")
msg ="Please choose an action?"
title = "Critical Situation Detected!"
choices = ["Ignore the Warning", "Contact Doctor", "Call Ambulance Service", "Call Hospital"]
#choice = choicebox(msg, title, choices)
choice = multchoicebox(msg, title, choices)
#stop alarm sound
a.close()
# note that we convert choice to string, in case
# the user cancelled the choice, and we got None.
msgbox("You chose: " + str(choice), "Action is in Progress")
msg = "Do you want to continue?"
title = "Please Confirm"
if ccbox(msg, title): # show a Continue/Cancel dialog
pass # user chose Continue
else:
sys.exit(0) # user chose Cancel
的AudioFile:
import pyaudio
import wave
import sys
class AudioFile:
chunk = 1024
def __init__(self, file):
""" Init audio stream """
self.wf = wave.open(file, 'rb')
self.p = pyaudio.PyAudio()
self.stream = self.p.open(
format = self.p.get_format_from_width(self.wf.getsampwidth()),
channels = self.wf.getnchannels(),
rate = self.wf.getframerate(),
output = True
)
def play(self):
""" Play entire file """
data = self.wf.readframes(self.chunk)
while data != '':
self.stream.write(data)
data = self.wf.readframes(self.chunk)
def close(self):
""" Graceful shutdown """
self.stream.close()
self.p.terminate()
# Usage example for pyaudio
#a = AudioFile("alarm.wav")
#a.play()
#a.close()
当我使用主文件运行这两个代码时我想首先运行警报声并且在后台运行警报声应该出现在窗口中当我从第二个窗口中选择选项并按下确定它应该停止警报声但不是第一次我的应用程序在它结束后播放警报声音启动gui。我应该如何在gui的背景中播放我的闹钟声音并在按下第二个ok按钮后将其关闭?
答案 0 :(得分:1)
目前,当您运行AudioFile.play()
方法时,您将在执行msgbox("Critical Situation Detected!")
命令之前播放整个文件。
解决方案是在一个线程中运行警报,使控件保留在主文件的while循环中。
线程警报的外观(减去详细信息)的示例如下:
from threading import Thread,Event
from time import sleep
class AudioFile(Thread):
def __init__(self):
Thread.__init__(self)
self._stop = Event()
def run(self):
self._stop.clear()
# in this case we loop until the stop event is set
while not self._stop.is_set():
print "BEEP BEEP BEEP!"
sleep(0.2)
def stop(self):
self._stop.set()
然后在您的主要代码中,您可以a.play
和a.close
替换a.start
和a.stop
。例如:
x = AudioFile()
x.start()
sleep(4)
x.stop()
答案 1 :(得分:0)
我想出了基于@ebarr示例代码的解决方案。
主文件:
predictions[0] = 1
a = AudioFile("alarm.wav")
if (predictions[0] == 1):
while 1:
a.start()
sleep(0.5)
msgbox("Critical Situation Detected!")
msg ="Please choose an action?"
title = "Critical Situation Detected!"
choices = ["Ignore the Warning", "Contact Doctor", "Call Ambulance Service", "Call Hospital"]
#choice = choicebox(msg, title, choices)
choice = multchoicebox(msg, title, choices)
a.stop()
# note that we convert choice to string, in case
# the user cancelled the choice, and we got None.
msgbox("You chose: " + str(choice), "Action is in Progress")
msg = "Do you want to continue?"
title = "Please Confirm"
if ccbox(msg, title): # show a Continue/Cancel dialog
pass # user chose Continue
else:
sys.exit(0) # user chose Cancel
的AudioFile:
import pyaudio
import wave
import sys
from threading import Thread,Event
from time import sleep
class AudioFile(Thread):
chunk = 1024
def __init__(self, file):
""" Init audio stream """
Thread.__init__(self)
self.wf = wave.open(file, 'rb')
self.p = pyaudio.PyAudio()
self.stream = self.p.open(
format = self.p.get_format_from_width(self.wf.getsampwidth()),
channels = self.wf.getnchannels(),
rate = self.wf.getframerate(),
output = True
)
self._stop = Event()
def run(self):
self._stop.clear()
""" Play entire file """
while not self._stop.is_set():
data = self.wf.readframes(self.chunk)
self.stream.write(data)
def stop(self):
""" Graceful shutdown """
self._stop.set()
self.stream.close()
self.p.terminate()