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

Boron :

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.

Oush :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=70789&siteId=1