在TextToSpeech发言之前我必须等待几秒钟

时间:2016-05-11 12:50:37

标签: android text time voice speech

我的TextToSpeech有问题。事实上,当我调用我的功能“Son”(见下文)时(例如我点击),我必须等待4秒才能听到第一次点击声音,但是在第一次点击后,它会立即听到声音。

但有时它从第一次点击开始就完美无缺。在Android监视器中,如果它有效,我可以看到:

   I/TextToSpeech: Sucessfully bound to com.google.android.tts
   I/TextToSpeech: Connected to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}
   I/TextToSpeech: Set up connection to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}

所以我认为这取决于Android,但我希望我能做些什么来纠正这个......你有什么想法吗? 如果您需要更多信息,请不要犹豫!

谢谢你们!

我的代码:

   public void Son(final String texte_son){
    t1=new TextToSpeech(this, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if(status != TextToSpeech.ERROR) {
                t1.setLanguage(Locale.FRENCH);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    ttsGreater21(texte_son);
                } else {
                    ttsUnder20(texte_son);
                }
            }
        }
    });
}
@SuppressWarnings("deprecation")
private void ttsUnder20(String text) {
    HashMap<String, String> map = new HashMap<>();
    map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "MessageId");
    t1.speak(text, TextToSpeech.QUEUE_FLUSH, map);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void ttsGreater21(String text) {
    String utteranceId=this.hashCode() + "";
    t1.speak(text, TextToSpeech.QUEUE_FLUSH, null, utteranceId);
}

1 个答案:

答案 0 :(得分:1)

应用程序启动时的Init TTS并将指针存储到它,每次需要合成脚本时都不要创建TTS

public YourActivity implements Activity {

   private Tts tts;

   void onCreate() {
       tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
           @Override
           public void onInit(int status) {
               tts.setLanguage(Locale.FRENCH);
           }
       });
   }

   void Son(String text) {
      if (tts != null) {
          HashMap<String, String> map = new HashMap<>();
          map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "MessageId");
         tts.speak(text, TextToSpeech.QUEUE_FLUSH, map);
      }
   }
}
相关问题