From ab089078b1d42f8ecf549669847b47c0c2c5854b Mon Sep 17 00:00:00 2001 From: Charles Chen Date: Fri, 11 Dec 2009 13:54:11 -0800 Subject: Fix for a race condition that can occur if an utterance is stopped right when it completes. --- .../TtsService/src/android/tts/TtsService.java | 37 ++++++++++++---------- 1 file changed, 21 insertions(+), 16 deletions(-) (limited to 'packages') diff --git a/packages/TtsService/src/android/tts/TtsService.java b/packages/TtsService/src/android/tts/TtsService.java index 217d3bb..5c4bbb8 100755 --- a/packages/TtsService/src/android/tts/TtsService.java +++ b/packages/TtsService/src/android/tts/TtsService.java @@ -596,22 +596,27 @@ public class TtsService extends Service implements OnCompletionListener { } } - public void onCompletion(MediaPlayer arg0) { - String callingApp = mCurrentSpeechItem.mCallingApp; - ArrayList params = mCurrentSpeechItem.mParams; - String utteranceId = ""; - if (params != null){ - for (int i = 0; i < params.size() - 1; i = i + 2){ - String param = params.get(i); - if (param.equals(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID)){ - utteranceId = params.get(i+1); - } - } - } - if (utteranceId.length() > 0){ - dispatchUtteranceCompletedCallback(utteranceId, callingApp); - } - processSpeechQueue(); + public void onCompletion(MediaPlayer arg0) { + // mCurrentSpeechItem may become null if it is stopped at the same + // time it completes. + SpeechItem currentSpeechItemCopy = mCurrentSpeechItem; + if (currentSpeechItemCopy != null) { + String callingApp = currentSpeechItemCopy.mCallingApp; + ArrayList params = currentSpeechItemCopy.mParams; + String utteranceId = ""; + if (params != null) { + for (int i = 0; i < params.size() - 1; i = i + 2) { + String param = params.get(i); + if (param.equals(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID)) { + utteranceId = params.get(i + 1); + } + } + } + if (utteranceId.length() > 0) { + dispatchUtteranceCompletedCallback(utteranceId, callingApp); + } + } + processSpeechQueue(); } private int playSilence(String callingApp, long duration, int queueMode, -- cgit v1.1