Search code examples
javaandroidfor-loopwhile-looptext-to-speech

Reading Number of "Strings" in TTS


I have number of Strings, which contain 5 sentences each. Now, I want to pass these to the android Text To Speech, one by one, one at a time. Which means, the first String get passed to the engine, and the second text should be passed once the engine has completed speaking. Below is my code.

List<String>textCollection = new ArrayList<String>();

//Add sentences to 'textCollection '. Code removed//

for(int i=0;i<textCollection.size();i++)
{
    while(tts.isSpeaking())
    {

     }

    Toast.makeText(Talk.this, ""+i, Toast.LENGTH_LONG).show();

    new SpeakTheText().execute(textCollection.get(i));

}

//This class will speak the text
    private class SpeakTheText extends AsyncTask<String,Void,String>
    {

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub

            tts.speak(params[0], TextToSpeech.QUEUE_FLUSH, null);
            return null;
        }

    }

Now, unfortunately, what is happening is unexpected. The Voice engine just speak text from here and there, not in order! It never speaks the first text, just pick a text from somewhere and read it. Why is this happening?


Solution

  • You want to change parameter of tts

    tts.speak(params[0], TextToSpeech.QUEUE_ADD, null);
    

    in my application work workwell.