Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text To Speech (TTS) delays by 3 seconds to speak text

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.

like image 488
Boron Avatar asked Nov 07 '22 23:11

Boron


1 Answers

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

like image 188
Oush Avatar answered Nov 14 '22 22:11

Oush