here is my code,
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
initialized = true;
tts.setLanguage(Locale.ENGLISH);
if (queuedText != null) {
speak(queuedText);
}
}
}
public void speak(String text) {
// If not yet initialized, queue up the text.
if (!initialized) {
queuedText = text;
return;
}
queuedText = null;
// Before speaking the current text, stop any ongoing speech.
tts.stop();
// Speak the text.
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
I am calling the speak function like this,
speak("ten");
speak("nine");
I am able to hear only nine.I am trying to implement a counter, I will be adding delays between the calls.
There are, roughly, two related problems with how you're trying to speak things in sequence:
tts.speak() is an async operation. It doesn't actually do the audio at that point, but queues it for the audio system to play. It usually plays shortly afterwards, but there is no guarantee that it will start speaking immediately.
The call tts.stop() stops any speaking that was currently in progress, and using QUEUE_FLUSH means that any text that hadn't been playing yet won't be. So if you're calling speak("nine")
immediately after speak("ten")
, the "nine" immediately removes the "ten" from the queue.
You have a couple of options, tho I don't know which one will meet your needs best.
So changing the code to something like this, should work:
public void speak(String text) {
// If not yet initialized, queue up the text.
if (!ttsInitialized) {
queuedText = text;
return;
}
queuedText = null;
// Before speaking the current text, stop any ongoing speech.
//tts.stop();
// Speak the text.
tts.speak(text, TextToSpeech.QUEUE_ADD, null);
}
Code to do this might look something like
private void countdown( final int start ){
speak( ""+start );
if( start > 0 ){
Handler handler = new Handler();
handler.postDelayed( new Runnable(){
@Override
public void run(){
countdown( start-1 );
}
}, 1000 );
}
}
and be called through
countdown(10);