0

Following the advice from https://stackoverflow.com/a/22875621/9023855 why isn't my code working as expected? My plan is to auto-click button.

private boolean threadDone = true;

public void setTimer(final boolean shown_answer, final TextToSpeech tts){

    final Handler h =new Handler();
    Runnable r = new Runnable() {
        public void run() {
            if (!tts.isSpeak() && threadDone) {
                onTtsFinished(shown_answer);
            }

            h.postDelayed(this, 1000);
        }
    };
    h.postDelayed(r, 1000);
}

public void onTtsFinished(boolean shown_answer) {
    threadDone = false;
    if(showAnswer && !shown_answer){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                button.performClick();
                threadDone = true;
            }
        }, showDelay);

    }

    if(nextQuestion && shown_answer){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                button.performClick();
                threadDone = true;
            }
        }, nextDelay);
    }
}

https://github.com/patarapolw/AndroidDuendecat/blob/052d228165277c655417e4f4fb586295b7e710c8/app/src/main/java/com/blogspot/fossipol/duendecat/MyTimer.java#L54

Polv
  • 1,918
  • 1
  • 20
  • 31
  • You don't need to create multiple instances of the TTS Object to speak multiple utterances. Just use one and call `speak()` multiple times. Also, you should use an `UtteranceProgressListener` to correctly handle the 'progress' of the speech. – brandall Jan 21 '18 at 07:22
  • I just use multiple TTS object to do multiple languages, but now I can unify the TTS to one. But still, somehow, UtteranceProgressListener doesn't work for me... but I'll look into it for a better timing. – Polv Jan 21 '18 at 18:42
  • Why is `UtteranceProgressListener` recommended over `isSpeak()`? – Polv Jan 23 '18 at 11:02
  • 1
    From your code, you appear to be interested in when the engine 'stops speaking', rather than 'is speaking'. This callback is available in the `UtteranceProgressListener` as well as others. Using multiple threads and handlers to monitor this is a waste of resource. The `UtteranceProgressListener` is very easy to use and exactly for this purpose. – brandall Jan 24 '18 at 13:00
  • Only if I don't have to monitor `threadDone` variable in `onTtsFinished`, and my attempt at setting a boolean listener (using interface) failed. I do able to get `UtteranceProgressListener` to work, though. – Polv Jan 24 '18 at 22:00

0 Answers0