-1

こんにちは、アラーム音と同時に GUI を実行し、2 番目のダイアログ ボックスで [OK] ボタンをクリックすると、アラーム音の繰り返しを停止する必要があります。このタスクを達成するために、メインファイル(easyguiを使用したgui )である2つのファイルを作成し、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()

メインファイルを使用してこの2つのコードを実行すると、最初にアラーム音を実行し、バックグラウンドでGUIがウィンドウに表示され、2番目のウィンドウから選択肢を選択して[OK]を押すと、アラーム音が停止するはずですが、代わりに最初に私のアプリケーションがアラーム音を鳴らした後、GUI を開始します。GUI のバックグラウンドでアラーム音を再生し、2 番目の OK ボタンを押した後に閉じるにはどうすればよいですか?

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.play次に、メイン コードで anda.closea.startandに置き換えますa.stop。例えば:

x = AudioFile()
x.start()
sleep(4)
x.stop()
于 2014-05-23T05:49:48.807 に答える