文本到语音初始化延迟

时间:2016-01-19 19:04:41

标签: android text-to-speech speech-to-text google-text-to-speech

我正在尝试为我的应用添加文字转语音功能,并且在我从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);
           }
         }
    });

}

非常感谢

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

您可以像这样在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);
        }
    }