I am using the TextToSpeak feature in my android app and realized its taking up some delay before speaking out the actual word.
onCreate(){
textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
textToSpeech.setLanguage(Locale.UK);
}
}
});
textToSpeech.speak("Hello World", TextToSpeech.QUEUE_FLUSH, null, null);
performAction();
}
performAction(){…}
As you can see I am then calling the performAction method immediately after using TTS .speak() method, but the 3 second delay causes some inaccuracy.
How can I trigger the performAction method to be called immediately the word is spoken out.
This is probably not the most efficient way of doing it,I had a similar problem to this and used a Handler to solve it.
onCreate(){
textToSpeech = new TextToSpeech(getApplicationContext(), new
TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
textToSpeech.setLanguage(Locale.UK);
}
}
});
textToSpeech.speak("Hello World", TextToSpeech.QUEUE_FLUSH, null, null);
checkIfTTSIsSpeaking();
}
checkIfTTSIsSpeaking() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if(textToSpeech.isSpeaking()){
performAction();
}else{
checkIfTTSIsSpeaking();
}
}
},10);
}
performAction(){…}
TextToSpeech engine has a method isSpeaking()
which returns a boolean whether or not the word is being spoken.
TextToSpeech reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With