TTS: Success returned from onInit (), but there is still a delay for the first performance

Used code:

public class TexttoSpeechActivity extends Activity implements OnInitListener { private TextToSpeech tts; private Button btnSpeak; private EditText txtText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tts = new TextToSpeech(this, this); btnSpeak = (Button) findViewById(R.id.btnSpeak); txtText = (EditText) findViewById(R.id.txtText); btnSpeak.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { speakOut(); } }); } @Override public void onDestroy() { // Don't forget to shutdown! if (tts != null) { tts.stop(); tts.shutdown(); } super.onDestroy(); } 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(this, "Language not supported", Toast.LENGTH_LONG).show(); Log.e("TTS", "Language is not supported"); } else { btnSpeak.setEnabled(true); } } else { Log.e("TTS", "Initilization Failed"); } } private void speakOut() { String text = txtText.getText().toString(); if (text.length() == 0) { tts.speak("You haven't typed text", TextToSpeech.QUEUE_FLUSH, null); } else { tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); } System.out.println("apr2 tts isSpeaking "+tts.isSpeaking()); } } 

success status is returned from onInit (). But there are some delays for the first performance. those. if you immediately click on the start button to speak, tts does not work. After some delay (10-15 seconds) it works fine.

I can use a handler to wait 10-15 seconds, as described in Text to Speech does not work as expected . But this is not good. Since this delay of 10-15 seconds can vary from device to device. Does anyone know how to handle this first delay.

+5
source share

Source: https://habr.com/ru/post/1216664/


All Articles