为什么Android文本到语音初始化很慢?
我正在我的Android应用程序中实现TTS(文本到语音)支持。我正在显示我的代码:为什么Android文本到语音初始化很慢?,android,text-to-speech,android-speech-api,Android,Text To Speech,Android Speech Api,我正在我的Android应用程序中实现TTS(文本到语音)支持。我正在显示我的代码: TextSpeech textSpeech = new TextSpeech(); textToSpeech = new TextToSpeech(this, textSpeech); private class TextSpeech implements TextToSpeech.OnInitListener { @Override public void onInit(final i
TextSpeech textSpeech = new TextSpeech();
textToSpeech = new TextToSpeech(this, textSpeech);
private class TextSpeech implements TextToSpeech.OnInitListener {
@Override
public void onInit(final int status) {
if (status != TextToSpeech.ERROR) {
if (TextToSpeech.LANG_AVAILABLE == textToSpeech.isLanguageAvailable(Locale.ENGLISH)) {
Thread thread = new Thread() {
@Override
public void run() {
super.run();
textToSpeech.setLanguage(Locale.ENGLISH);
try {
for (int i = 0; i < 10; i++) {
if (Util.isGreaterThanApi21()) {
textToSpeech.speak("" + i, TextToSpeech.QUEUE_FLUSH, null, null);
} else {
textToSpeech.speak("" + i, TextToSpeech.QUEUE_FLUSH, null);
}
Thread.sleep(1000);
}
} catch (Exception exception) {
}
}
};
thread.start();
} else {
Toast.makeText(ViewWorkplanActivity.this, "Language Not Supported", Toast.LENGTH_SHORT).show();
}
}
}
}
TextSpeech TextSpeech=newtextspeech();
textToSpeech=新textToSpeech(这是textSpeech);
私有类TextSpeech实现TextToSpeech.OnInitListener{
@凌驾
公共无效onInit(最终int状态){
if(状态!=TextToSpeech.ERROR){
if(TextToSpeech.LANG_AVAILABLE==TextToSpeech.isLanguageAvailable(Locale.ENGLISH)){
线程线程=新线程(){
@凌驾
公开募捐{
super.run();
textToSpeech.setLanguage(Locale.ENGLISH);
试一试{
对于(int i=0;i<10;i++){
if(Util.isGreaterThanApi21()){
textToSpeech.speak(“+i,textToSpeech.QUEUE\u FLUSH,null,null);
}否则{
textToSpeech.speak(“+i,textToSpeech.QUEUE\u FLUSH,null);
}
睡眠(1000);
}
}捕获(异常){
}
}
};
thread.start();
}否则{
Toast.makeText(viewworkanactivity.this,“不支持语言”,Toast.LENGTH_SHORT.show();
}
}
}
}
当我运行此代码时,一些设备缺少3-4个数字或需要3-4秒进行初始化。有谁能告诉我如何删除此错误吗?@FrankN.Stein当我运行此代码时,它应该从0到9开始计数,但从4开始计数。我想知道为什么初始化需要时间?@FrankN.Stein此延迟时间在不同的设备上有所不同。我已在Micromax Canvas Unite 2设备-->4秒Redmi 2-->0秒Moto E第二代-->8秒上进行了检查,我将使用
异步任务
加载TTS引擎。然后,在onPostExecute()
中启动说出数字的方法。@FrankN.Stein你能把你的代码发给我吗?我没有。这只是一个关于如何做到这一点的想法。