文字转语音 (TTS) 延迟 3 秒才能说出文字

Bor*_*ron 5 java android text-to-speech

我在我的 android 应用程序中使用 TextToSpeak 功能,并意识到它在说出实际单词之前需要一些延迟。

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(){…}
Run Code Online (Sandbox Code Playgroud)

如您所见,我在使用 TTS .speak() 方法后立即调用 performAction 方法,但 3 秒延迟会导致一些不准确。

我怎样才能触发 performAction 方法来立即调用这个词。

Ous*_*ush 1

这可能不是最有效的方法,我有一个类似的问题,并使用处理程序来解决它。

\n\n
onCreate(){\n  textToSpeech = new TextToSpeech(getApplicationContext(), new \n  TextToSpeech.OnInitListener() {\n    @Override\n    public void onInit(int status) {\n        if (status != TextToSpeech.ERROR) {\n            textToSpeech.setLanguage(Locale.UK);\n        }\n    }\n  });\n\n  textToSpeech.speak("Hello World", TextToSpeech.QUEUE_FLUSH, null, null);\n  checkIfTTSIsSpeaking();\n}\n\ncheckIfTTSIsSpeaking() {\n    new Handler().postDelayed(new Runnable() {\n    @Override\n    public void run() {\n      if(textToSpeech.isSpeaking()){\n         performAction();\n      }else{\n         checkIfTTSIsSpeaking();\n      }\n     }\n    },10);\n}\n\n\nperformAction(){\xe2\x80\xa6}\n
Run Code Online (Sandbox Code Playgroud)\n\n

TextToSpeech 引擎有一个方法isSpeaking(),无论是否正在说出单词,都会返回布尔值。

\n\n

文本转语音参考

\n