androidandroid-emulatorhashmaptext-to-speechonutterancecompleted

onUtteranceCompleted gets fired on emulator, but not on hardware


I can't get my onUtteranceCompleted() get fired on my Galaxy Nexus 4.0.2. My emulators with api 8, 10 and 15 do fire onUtteranceCompleted().

edit: well.. the statement above is true for most cases, I just got it to work on my Hardware 4.0.2. Then I closed it and started it again, and onUtteranceCompleted() did not get fired again. Had the same thing yesterday (before some code changes), so it's not working 90% of the time. Can't figure it out ;(

edit2:FYI: the mTts.setOnUtteranceCompletedListener(this); returns TextToSpeech.SUCCESS

Here is my code:

(...)
    public void onInit(int status) {            
mTts.setOnUtteranceCompletedListener(this);

if (status == TextToSpeech.SUCCESS) {    
    int result = mTts.setLanguage(Locale.US);
    if (result == TextToSpeech.LANG_MISSING_DATA ||
        result == TextToSpeech.LANG_NOT_SUPPORTED) {
        Log.e(TAG, "Language is not available.");
    } else {
        TTSAusgabe.setEnabled(true);
    }
} else {
    Log.e(TAG, "TTS failed");
}    
}


SayText() {  (....)
MundAnimation.start();
HashMap<String, String> params = new HashMap<String, String>();
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "utterance");
mTts.speak("Ma Text", TextToSpeech.QUEUE_FLUSH, params);
  } 
}


// That's the bad boy!
public void onUtteranceCompleted(String utterance) 
{
MundAnimation.stop();
    //startVoiceRecognitionActivity();
System.out.println("drin"); 
}
(...)

Solution

  • Since you have a problem that only occurs sometimes the problem must be due to the asynchronous nature of the TextToSpeech initialization. I have seen TextToSpeech fail sometimes when my code sets a property of TextToSpeech before onInit() is called.

    I suspect that you activated SayText() before TextToSpeech called onInit(). This will happen only some times.

    It looks like you have the right code that sets the onUtteranceCompletedListener during onInit() though, but perhaps that has a timing problem as well.