Question

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(). I've invested about 5 hours and read every single post on stackoverflow about this and I don't get it to work. I would be so happy if someone can help me out ;( I'm pretty new to android, so it would be awesome if you could explain it for beginners.

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 its not working 90% of the time. Can't figure it out ;(

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

Thanks a lot!

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"); 
}
(...)
Était-ce utile?

La 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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top