こんにちは、アラーム音と同時に 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 ボタンを押した後に閉じるにはどうすればよいですか?