我正在尝试为我的应用程序添加文本到语音的功能,在我从Google Play商店更新TTS之前,它一直运行得很好。
在onCreate方法中初始化TTS没有任何延迟。更新后,此TTS需要3-5秒才能完成初始化。基本上,文本到语音转换直到3-5秒后才准备就绪。
有人能告诉我我做错了什么吗?
private HashMap<String, String> TTS_ID = new HashMap<String, String>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.....
.....
TextToSpeech_Initialize();
}
public void TextToSpeech_Initialize() {
TTS_ID.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "UniqueID");
speech = new TextToSpeech(MainActivity.this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status == TextToSpeech.SUCCESS) {
speech.setSpeechRate(SpeechRateValue);
speech.speak(IntroSpeech, TextToSpeech.QUEUE_FLUSH, TTS_ID);
}
}
});
}
非常感谢
发布于 2017-01-29 00:39:59
确认!这是一个谷歌文本到语音引擎的问题,如果你尝试任何其他的tts,延迟消失,例如Pico tts。
发布于 2019-09-05 09:09:26
我以前遇到过这个问题,但现在我找到了一个合适的解决办法。
您可以在onCreate()中像这样初始化TextToSpeach:
TextToSpeach textToSpeech = new TextToSpeech(this, this);
但首先需要implement TextToSpeech.OnInitListener
,然后需要覆盖onInit()
方法:
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(getApplicationContext(), "Language not supported", Toast.LENGTH_SHORT).show();
} else {
button.setEnabled(true);
}
} else {
Toast.makeText(getApplicationContext(), "Init failed", Toast.LENGTH_SHORT).show();
}
}
我还注意到,如果您没有在onInit()
中设置语言,将会有延迟!!
,现在您可以编写表示文本的方法:
private void speakOut(final String detectedText){
if(textToSpeech !=null){
textToSpeech.stop(); //stop and say the new word
textToSpeech.speak(detectedText ,TextToSpeech.QUEUE_FLUSH, null, null);
}
}
https://stackoverflow.com/questions/34884961
复制相似问题