OSDN Git Service

Fix for a race condition that can occur if an utterance
authorCharles Chen <clchen@google.com>
Fri, 11 Dec 2009 21:54:11 +0000 (13:54 -0800)
committerCharles Chen <clchen@google.com>
Fri, 11 Dec 2009 21:54:11 +0000 (13:54 -0800)
is stopped right when it completes.

packages/TtsService/src/android/tts/TtsService.java

index 217d3bb..5c4bbb8 100755 (executable)
@@ -596,22 +596,27 @@ public class TtsService extends Service implements OnCompletionListener {
         }
     }
 
-    public void onCompletion(MediaPlayer arg0) {
-        String callingApp = mCurrentSpeechItem.mCallingApp;
-        ArrayList<String> 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) {\r
+        // mCurrentSpeechItem may become null if it is stopped at the same\r
+        // time it completes.\r
+        SpeechItem currentSpeechItemCopy = mCurrentSpeechItem;\r
+        if (currentSpeechItemCopy != null) {\r
+            String callingApp = currentSpeechItemCopy.mCallingApp;\r
+            ArrayList<String> params = currentSpeechItemCopy.mParams;\r
+            String utteranceId = "";\r
+            if (params != null) {\r
+                for (int i = 0; i < params.size() - 1; i = i + 2) {\r
+                    String param = params.get(i);\r
+                    if (param.equals(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID)) {\r
+                        utteranceId = params.get(i + 1);\r
+                    }\r
+                }\r
+            }\r
+            if (utteranceId.length() > 0) {\r
+                dispatchUtteranceCompletedCallback(utteranceId, callingApp);\r
+            }\r
+        }\r
+        processSpeechQueue();\r
     }
 
     private int playSilence(String callingApp, long duration, int queueMode,