Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: waiting until tts speak is finished, then continue

Tags:

android

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?

like image 254
aoaoaoao Avatar asked Jan 25 '11 02:01

aoaoaoao


4 Answers

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);
        }
    });
like image 108
ruckc Avatar answered Oct 22 '22 15:10

ruckc


boolean speakingEnd = tts.isSpeaking();
do{
   speakingEnd = tts.isSpeaking();
} while (speakingEnd);
//Continue with code
like image 33
saad Avatar answered Oct 22 '22 17:10

saad


public void speak(String message){        
    tts.speak(message, TextToSpeech.QUEUE_FLUSH, null); 
    while (tts.isSpeaking()){
        System.Out.Println("Do something or nothing while speaking..")
    } 
}
like image 2
Keyur Avatar answered Oct 22 '22 15:10

Keyur


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();

        }
    };


}
like image 1
Mohammed Javad Avatar answered Oct 22 '22 16:10

Mohammed Javad