0

I am implementing TTS (Text to Speech) support in my Android app. I am showing my code :

TextSpeech textSpeech = new TextSpeech();

 textToSpeech = new TextToSpeech(this, textSpeech);

 private class TextSpeech implements TextToSpeech.OnInitListener {
    @Override
    public void onInit(final int status) {
        if (status != TextToSpeech.ERROR) {

            if (TextToSpeech.LANG_AVAILABLE == textToSpeech.isLanguageAvailable(Locale.ENGLISH)) {

                Thread thread = new Thread() {
                    @Override
                    public void run() {
                        super.run();

                        textToSpeech.setLanguage(Locale.ENGLISH);

                        try {
                            for (int i = 0; i < 10; i++) {
                                if (Util.isGreaterThanApi21()) {
                                    textToSpeech.speak("" + i, TextToSpeech.QUEUE_FLUSH, null, null);
                                } else {
                                    textToSpeech.speak("" + i, TextToSpeech.QUEUE_FLUSH, null);
                                }
                                Thread.sleep(1000);
                            }
                        } catch (Exception exception) {

                        }
                    }
                };

                thread.start();
            } else {
                Toast.makeText(ViewWorkplanActivity.this, "Language Not Supported", Toast.LENGTH_SHORT).show();
            }
        }

    }

}

When I run this code, some devices missing 3-4 number or taking 3-4 seconds for initialization. Can any body tell me how can I remove this error?

Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
Akashsingla19
  • 662
  • 1
  • 7
  • 17
  • @FrankN.Stein When I run this code, then it should speak counting from 0 -9, but it starts from count 4. I want to know why it take time for initialization? – Akashsingla19 Nov 17 '15 at 12:30
  • @FrankN.Stein this delay time differ on different device. I have checked on Micromax Canvas Unite 2 Device --> 4 sec Redmi 2 --> 0 sec Moto E 2nd gen --> 8 sec – Akashsingla19 Nov 17 '15 at 12:34
  • I'd use an `AsyncTask` to load the TTS engine. Then, in `onPostExecute()` start the method which speaks the numbers. – Phantômaxx Nov 17 '15 at 12:42
  • @FrankN.Stein Can you send me your code? – Akashsingla19 Nov 17 '15 at 13:12
  • I don't have it. It was just an idea on how you could do that. – Phantômaxx Nov 17 '15 at 13:26
  • problem is, it won't help. It's not slow to load the engine (or if it is, there's an progress listener), it's very slow to set the language (which has no "initialized" event!) – chksr Jul 24 '17 at 19:04

0 Answers0