1

我正在尝试将文本转语音功能添加到我的应用程序中,并且在我从 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);
           }
         }
    });

}

非常感谢

4

2 回答 2

2

确认的!这是 Google 文本到语音引擎的问题,如果您尝试任何其他 tts,延迟就会消失,例如 Pico tts。

于 2017-01-28T16:39:59.410 回答
-1

我以前偶然发现过这个问题,但现在我找到了一个合适的解决方案..

您可以像这样在 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);
        }
    }
于 2019-09-05T01:09:25.730 回答