Search code examples
javaandroid-studiotext-to-speech

Find out how many texttospeech 'items' are processed in the queue


I hope I can make myself clear here. I have an arraylist with text which I want to read out loud. When, for instance, with 75 objects in the list, after the third sentence spoken, I want to click 'STOP' and do something with the third position of the arraylist I can't find a way to find out the texttospeech queue has processed 3 lines of the queue?

if(!sh.Contents.isEmpty()) {
   for (String object: sh.Contents) {
       sh.CurChapter += 1;
       speech(String.valueOf(sh.CurChapter) + " " + object);
       //textToSpeech.speak(substr, TextToSpeech.QUEUE_ADD, null, null)
   }
} else {
       speech( "Section not found!");
}

Kind regards,

Mike


Solution

  • In your speech array loop, use the position as the utteranceId and set it:

    speak(utterances.get(i), TextToSpeech.QUEUE_ADD, yourBundle, String.valueOf(i))
    

    Then in your UtteranceProgressListener

    public static final int DO_ACTION_1 = 0;
    public static final int DO_ACTION_5 = 4;
    
        @Override
        public void onDone(final String utteranceId) {
    
                switch (Integer.valueOf(utteranceId)) {
    
                    case DO_ACTION_1:
                        // do something
                        break;
                    case DO_ACTION_5:
                        // do something else
                        break;
                    default:
                        // do nothing
                        break;
                }
        }