Android TTS fails to speak large amount of text

Use this code...Working for any file .. just send the string to speech function..

private void speech(String charSequence) {

    int position ;


    int sizeOfChar= charSequence.length();
    String testStri= charSequence.substring(position,sizeOfChar);


    int next = 20;
    int pos =0;
    while(true) {
        String temp="";
        Log.e("in loop", "" + pos);

        try {

      temp = testStri.substring(pos, next);
            HashMap<String, String> params = new HashMap<String, String>();
            params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, temp);
            engine.speak(temp, TextToSpeech.QUEUE_ADD, params);

            pos = pos + 20;
            next = next + 20;

        } catch (Exception e) {
            temp = testStri.substring(pos, testStri.length());
            engine.speak(temp, TextToSpeech.QUEUE_ADD, null);
            break;

        }

    }

}

The String length should not be longer than pre-defined length, from docs:

Parameters

text The string of text to be spoken. No longer than getMaxSpeechInputLength() characters.

Returned value by getMaxSpeechInputLength() may vary from device to device, but according to AOSP source that is whopping 4000:

/**
 * Limit of length of input string passed to speak and synthesizeToFile.
 *
 * @see #speak
 * @see #synthesizeToFile
 */
public static int getMaxSpeechInputLength() {
    return 4000;
}

Try not to exceed that limit: compare input text length with that value and split into separate parts if necessary.