I cannot figure out why TTS does not work consistently. I would like some insight on how to improve my current setup. The application begins with TTS, welcoming the user or prompting the user to say a voice command. So in onCreate I simply have the following:
textToSpeech = new TextToSpeech(TTSActivity.this, this);
My TTSActivity class implemetns OnInitListener, so I also have the following override method:
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {
// TODO Auto-generated method stub
}
@Override
public void onError(String utteranceId) {
// TODO Auto-generated method stub
}
@Override
public void onDone(String utteranceId) {
//do some work here
}
});
textToSpeech.setLanguage(Locale.US);
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "UniqueID");
ttsString(0);
} else if (status == TextToSpeech.ERROR){
//try to restart TTS
textToSpeech = new TextToSpeech(TTSActivity.this, this);
ttsString(0);
}
} //end onInit() method
Some explanations: The textToSpeech.setOnUtteranceProgressListener() is there so that I can activate voice recognition once TTS engine has stopped speaking. map is from HashMap map = new HashMap() and ttsString are all the strings I want the TTS to speak. So my ttsString(id#) method looks like the following. Before I make TTS speak, I call ttsString(id#) to set the string.
private void ttsString(int id)
{
switch (id)
{
case 0:{
metaString = "Say string one";
speakText(metaString);
break;
}
case 1:{
metaString = "Say string two";
speakText(metaString);
break;
}
case 2:{
metaString = "Say string three";
speakText(metaString);
break;
}
}
}
This is how I get TTS to speak:
private void speakText(String text) {
if(textToSpeech.isSpeaking()) {
return;
} else {
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, map);
}
}
I am not receiving TTS unbound errors, everything works at one point in time, but not consistently. In other words, sometimes the program will speak and sometimes it will not. What do you suggest I do? Re-structure?