Skip to content

Commit

Permalink
use player finished event instead of polling
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongyihui committed Mar 29, 2018
1 parent 8f66e71 commit 1d40089
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
7 changes: 2 additions & 5 deletions avs/interface/audio_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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()

Expand Down
12 changes: 6 additions & 6 deletions avs/interface/speech_recognizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 6 additions & 3 deletions avs/interface/speech_synthesizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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": {
Expand Down

0 comments on commit 1d40089

Please sign in to comment.