-1

嗨,当我单击第二个对话框中的确定按钮时,我需要同时运行我的 gui 和我的警报声并停止迭代警报声。为了完成这个任务,我创建了 2 个文件,它们是主文件(使用easygui的 gui)和 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

音频文件:

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。在我按下第二个确定按钮后,我应该如何在 gui 的背景中播放我的警报声并关闭它?

4

2 回答 2

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.playand替换a.closea.startand a.stop。例如:

x = AudioFile()
x.start()
sleep(4)
x.stop()
于 2014-05-23T05:49:48.807 回答
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

音频文件:

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()
于 2014-05-23T06:52:39.540 回答