Darksymphony
Darksymphony

Reputation: 2693

Android - text to speech determine end of process

As I have a text longer than the 4000 chars limit for TTS, I divided the string to more parts and I am adding these parts in cycle to tts queue, for example:

int pos = 0;

while(true) {

            String var = "";

            try {
                var = str.substring(pos, 3999);
                pos += 3999;
            } catch(Exception e) {
                var = str.substring(pos, str.length());
                break;
            }

           Bundle params = new Bundle();
        params.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "");
            tts.speak(var, TextToSpeech.QUEUE_ADD, params, "myID");
        }

However this works, but after the END of speech I need to change the stop button again for play button.

I am doing this button change in onDone().

              tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
                    @Override
                    public void onDone(String utteranceId) {
                         Log.d("Speak", "TTS finished");


                        runOnUiThread(new Runnable() {
                            public void run() {
                                Button view2 = findViewById(R.id.speech);
                                view2.setCompoundDrawablesWithIntrinsicBounds(R.drawable.play, 0, 0, 0);
                            }
                        });
                         }



                    @Override
                    public void onError(String utteranceId) {
                    }

                    @Override
                    public void onStart(String utteranceId) {
                    }
                });

The problem is, the onDone is calling after each Queue finishes. So if there are more parts in the queue, the onDone is called many times. I can't determine, when the last queue is processed so when to change the button.

Upvotes: 2

Views: 420

Answers (1)

Darksymphony
Darksymphony

Reputation: 2693

I think I have found a solution - by sending a different utteranceId in

tts.speak(var, TextToSpeech.QUEUE_ADD, params, id1);

and in next condition id2. Then checking in onDone the utteranceId and according it change the button.

Seems working very well.

Upvotes: 2

Related Questions