From d23e7c18fcc83d7f933fda5597481a0f1fa0d57a Mon Sep 17 00:00:00 2001 From: chgenly Date: Tue, 11 Sep 2012 10:52:40 -0700 Subject: [PATCH] Fixed problem with handling delay caused by getNextTilfeToPlay() Fixed problem where tts onUtteranceCompleted() was not called because utterance id was not set. Fixed synchronization problems. --- .../plus/voice/MediaCommandPlayerImpl.java | 20 +++++++++++-------- .../plus/voice/TTSCommandPlayerImpl.java | 7 +++++-- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/voice/MediaCommandPlayerImpl.java b/OsmAnd/src/net/osmand/plus/voice/MediaCommandPlayerImpl.java index 2645849cc0..6bc3414c2a 100644 --- a/OsmAnd/src/net/osmand/plus/voice/MediaCommandPlayerImpl.java +++ b/OsmAnd/src/net/osmand/plus/voice/MediaCommandPlayerImpl.java @@ -46,6 +46,7 @@ public class MediaCommandPlayerImpl extends AbstractPrologCommandPlayer implemen mediaPlayer = null; } + // Called from the calculating route thread. @Override public synchronized void playCommands(CommandBuilder builder) { filesToPlay.addAll(builder.execute()); @@ -79,7 +80,7 @@ public class MediaCommandPlayerImpl extends AbstractPrologCommandPlayer implemen } /** - * Called when the MediaPlayer is done. + * Called when the MediaPlayer is done. The call back is on the main thread. */ @Override public void onCompletion(MediaPlayer mp) { @@ -87,7 +88,6 @@ public class MediaCommandPlayerImpl extends AbstractPrologCommandPlayer implemen playQueue(); } - private void performDelays() { int sleep = 0; while (!filesToPlay.isEmpty() && filesToPlay.get(0).startsWith(DELAY_CONST)) { @@ -98,8 +98,10 @@ public class MediaCommandPlayerImpl extends AbstractPrologCommandPlayer implemen } } try { - if (sleep != 0) + if (sleep != 0) { + log.debug("Delaying "+sleep); Thread.sleep(sleep); + } } catch (InterruptedException e) { } } @@ -109,10 +111,7 @@ public class MediaCommandPlayerImpl extends AbstractPrologCommandPlayer implemen String f = filesToPlay.remove(0); if (f != null && voiceDir != null) { File file = new File(voiceDir, f); - if (file.exists()) - return file; - else - log.error("Unable to play, does not exist: "+file); + return file; } } return null; @@ -124,8 +123,13 @@ public class MediaCommandPlayerImpl extends AbstractPrologCommandPlayer implemen * @param file */ private void playFile(File file) { - log.debug("Playing file : " + file); //$NON-NLS-1$ + if (!file.exists()) { + log.error("Unable to play, does not exist: "+file); + playQueue(); + return; + } try { + log.debug("Playing file : " + file); //$NON-NLS-1$ mediaPlayer.reset(); mediaPlayer.setAudioStreamType(streamType); mediaPlayer.setDataSource(file.getAbsolutePath()); diff --git a/OsmAnd/src/net/osmand/plus/voice/TTSCommandPlayerImpl.java b/OsmAnd/src/net/osmand/plus/voice/TTSCommandPlayerImpl.java index 3ad601c670..6e5ee1dd5e 100644 --- a/OsmAnd/src/net/osmand/plus/voice/TTSCommandPlayerImpl.java +++ b/OsmAnd/src/net/osmand/plus/voice/TTSCommandPlayerImpl.java @@ -90,8 +90,9 @@ public class TTSCommandPlayerImpl extends AbstractPrologCommandPlayer { */ private int ttsRequests; + // Called from the calculating route thread. @Override - public void playCommands(CommandBuilder builder) { + public synchronized void playCommands(CommandBuilder builder) { if (mTts != null) { final List execute = builder.execute(); //list of strings, the speech text, play it StringBuilder bld = new StringBuilder(); @@ -101,6 +102,7 @@ public class TTSCommandPlayerImpl extends AbstractPrologCommandPlayer { if (ttsRequests++ == 0) requestAudioFocus(); log.debug("ttsRequests="+ttsRequests); + params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,""+System.currentTimeMillis()); mTts.speak(bld.toString(), TextToSpeech.QUEUE_ADD, params); // Audio focus will be released when onUtteranceCompleted() completed is called by the TTS engine. } @@ -160,8 +162,9 @@ public class TTSCommandPlayerImpl extends AbstractPrologCommandPlayer { } }); mTts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() { + // The call back is on a binder thread. @Override - public void onUtteranceCompleted(String utteranceId) { + public synchronized void onUtteranceCompleted(String utteranceId) { if (--ttsRequests == 0) abandonAudioFocus(); log.debug("ttsRequests="+ttsRequests);