I am trying to introduce text-to-speech to android technology in my work, but I encounter a strange error. I do not hear any sound from my code. The talk method only works if I put it in the onInit method, otherwise it does not say.
My code is as follows:
public class GameOverActivity extends Activity implements OnInitListener {
private TextToSpeech talker;
....
talker = new TextToSpeech(this, this);
say("Something",false);
...
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
talker.setLanguage(Locale.US);
}
else if (status == TextToSpeech.ERROR) {
Toast.makeText(this,"Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
}
void say(String text, boolean flush) {
if(flush == true)
{
talker.speak(text,TextToSpeech.QUEUE_FLUSH,null);
}
if(flush == false)
{
talker.speak(text,TextToSpeech.QUEUE_ADD,null);
}
}
The strange thing is that if I put the say method in onInit, it works great!
I carefully monitored the logarithm and here are the results:
TtsService.OnCreate () TT loading Started AudioTrack TTSService.setLanguage loaded in the USA setting the speech speed to 100
and then nothing happens.
Any idea what is wrong with the above code?
Thanks in advance!
source
share