From 1d40089b325ae27d9e8dbb1e487c2e43b9a1510d Mon Sep 17 00:00:00 2001 From: Yihui Xiong Date: Thu, 29 Mar 2018 10:54:38 +0800 Subject: [PATCH] use player finished event instead of polling --- avs/interface/audio_player.py | 7 ++----- avs/interface/speech_recognizer.py | 12 ++++++------ avs/interface/speech_synthesizer.py | 9 ++++++--- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/avs/interface/audio_player.py b/avs/interface/audio_player.py index a4d19f3..50580ff 100644 --- a/avs/interface/audio_player.py +++ b/avs/interface/audio_player.py @@ -119,8 +119,8 @@ def __init__(self, alexa): # } # } def Play(self, directive): - while self.alexa.SpeechSynthesizer.state == 'PLAYING': - time.sleep(0.01) + if self.alexa.SpeechSynthesizer.state == 'PLAYING': + self.alexa.SpeechSynthesizer.wait() behavior = directive['payload']['playBehavior'] self.token = directive['payload']['audioItem']['stream']['token'] @@ -207,9 +207,6 @@ def PlaybackFailed(self): # } # } def Stop(self, directive): - # while self.alexa.SpeechSynthesizer.state == 'PLAYING': - # time.sleep(0.1) - self.player.stop() self.PlaybackStopped() diff --git a/avs/interface/speech_recognizer.py b/avs/interface/speech_recognizer.py index 8d9c6e7..5c948bf 100644 --- a/avs/interface/speech_recognizer.py +++ b/avs/interface/speech_recognizer.py @@ -2,14 +2,15 @@ """https://developer.amazon.com/public/solutions/alexa/alexa-voice-service/reference/speechrecognizer""" -import time import logging -import uuid +import sys +import time import threading +import uuid -try: +if sys.version_info < (3, 0): import Queue as queue -except ImportError: +else: import queue logger = logging.getLogger('SpeechRecognizer') @@ -65,8 +66,7 @@ def Recognize(self, dialog=None, initiator=None, timeout=10000): def on_finished(): if self.alexa.SpeechSynthesizer.state == 'PLAYING': logger.info('wait until speech synthesizer is finished') - while self.alexa.SpeechSynthesizer.state == 'PLAYING': - time.sleep(0.01) + self.alexa.SpeechSynthesizer.wait() logger.info('synthesizer is finished') with self.lock: diff --git a/avs/interface/speech_synthesizer.py b/avs/interface/speech_synthesizer.py index 4ddd610..286a1f2 100644 --- a/avs/interface/speech_synthesizer.py +++ b/avs/interface/speech_synthesizer.py @@ -30,10 +30,13 @@ def __init__(self, alexa): self.mp3_file = None def stop(self): - # self.finished.set() + self.finished.set() self.player.stop() self._state = 'FINISHED' + def wait(self): + self.finished.wait() + # { # "directive": { # "header": { @@ -68,7 +71,7 @@ def Speak(self, directive): if os.path.isfile(mp3_file): self.mp3_file = mp3_file - # self.finished.clear() + self.finished.clear() self.SpeechStarted() # os.system('mpv "{}"'.format(mp3_file)) self.player.play('file://{}'.format(mp3_file)) @@ -98,7 +101,7 @@ def SpeechFinished(self): if os.path.isfile(self.mp3_file): os.system('rm -rf "{}"'.format(self.mp3_file)) - # self.finished.set() + self.finished.set() self._state = 'FINISHED' event = { "header": {