aoaoaoao
aoaoaoao

Reputation: 61

Android: waiting until tts speak is finished, then continue

I'm really struggling with something... I have a couple of sentences that I want to read, both verbally through tts speek function, and via text on screen, one sentence at a time.

I have the textview area ready, but putting it together is what I'm not getting. Either it will read all the sentences and only show the last one, or it will show and read only the first sentence.

Anyone know i how I can accomplish this goal?

Upvotes: 6

Views: 8381

Answers (4)

Mohammed Javad
Mohammed Javad

Reputation: 163

Try this

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener{


    private boolean initialized;
    private String queuedText;
    private String TAG = "TTS";
    private TextToSpeech tts;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        tts = new TextToSpeech(this /* context */, this /* listener */);
        tts.setOnUtteranceProgressListener(mProgressListener);


        speak("hello world");

    }




    public void speak(String text) {

        if (!initialized) {
            queuedText = text;
            return;
        }
        queuedText = null;

        setTtsListener(); // no longer creates a new UtteranceProgressListener each time
        HashMap<String, String> map = new HashMap<String, String>();
        map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "MessageId");
        tts.speak(text, TextToSpeech.QUEUE_ADD, map);
    }


    private void setTtsListener() {

    }





    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            initialized = true;
            tts.setLanguage(Locale.ENGLISH);

            if (queuedText != null) {
                speak(queuedText);
            }
        }
    }



    private abstract class runnable implements Runnable {
    }




    private UtteranceProgressListener mProgressListener = new UtteranceProgressListener() {
        @Override
        public void onStart(String utteranceId) {
        } // Do nothing

        @Override
        public void onError(String utteranceId) {
        } // Do nothing.

        @Override
        public void onDone(String utteranceId) {

            new Thread()
            {
                public void run()
                {
                    MainActivity.this.runOnUiThread(new runnable()
                    {
                        public void run()
                        {

                            Toast.makeText(getBaseContext(), "TTS Completed", Toast.LENGTH_SHORT).show();

                        }
                    });
                }
            }.start();

        }
    };


}

Upvotes: 1

Keyur
Keyur

Reputation: 21

public void speak(String message){        
    tts.speak(message, TextToSpeech.QUEUE_FLUSH, null); 
    while (tts.isSpeaking()){
        System.Out.Println("Do something or nothing while speaking..")
    } 
}

Upvotes: 2

ruckc
ruckc

Reputation: 543

I just ran into this issue, according to the speak method, use an UtteranceProgressListener. I found out this is not executed on the UI thread, so I had to use runOnUiThread() to get back to update the activity.

tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
        @Override
        public void onStart(String utteranceId) {

        }

        @Override
        public void onDone(String utteranceId) {
            LettersActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // Do something on UI thread
                }
            });
        }

        @Override
        public void onError(String utteranceId) {
            Log.e(TAG, "error on " + utteranceId);
        }
    });

Upvotes: 5

saad
saad

Reputation: 33

boolean speakingEnd = tts.isSpeaking();
do{
   speakingEnd = tts.isSpeaking();
} while (speakingEnd);
//Continue with code

Upvotes: 3

Related Questions