Hi I need to run my gui simultaneously with my alarm sound and stop the iterating alarm sound when i click ok button in the 2nd dialog box. To achieve this task i created 2 files which is the main file(gui using easygui) and the AudioFile class witch uses pyaudio to play and stop alarm sound.
main file:
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()
When i run this two codes using main file i wanted to run the alarm sound first and in background the gui should appear in the window and when i select the choices from the second window and i press ok it should stop the alarm sound but instead of that first my application play the alarm sound after it is over it start the gui. how should i play my alarm sound in the background of the gui and close it after i press the second ok button?
I came up with the solution based on @ebarr sample code.
main file:
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()