Search code examples
androidtext-to-speech

TTS leaked service connection that was orignially bound here


I'm building an app in which there is tts part, in the main activity i'm creating an instance of tts like

public class Translator extends Activity implements OnClickListener{
TextToSpeech tts;
ArrayList<TTS.resultData> textsToBeSpoken = new ArrayList<TTS.resultData>();
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_translator);
        initTTS();//calling to initialise tts;
    }
}

and i'm calling the initTTS() method from onCreate() method;

public void initTTS(){
    tts = new TextToSpeech(Translator.this, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            toast("TTS ready to use");
            tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {

                @Override
                public void onStart(String utteranceId) {
                    log("Started speaking");
                }

                @Override
                public void onError(String utteranceId) {
                    log("Error in processing Text to speech");
                }

                @Override
                public void onDone(String utteranceId) {
                    log("Text to speech finished previewing");
                }

            });
        }
    });
}

and there is one more function called

public void speakUpSon(){
    HashMap<String, String> params = new HashMap<String, String>();

    if(textsToBeSpoken.size() > 0){
        for(int i = 0; i < textsToBeSpoken.size(); i++){
            if(getCanProceedSpeaking()){
                int index = i;
                params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "finished Speaking of index : " + i);
                System.out.println(textsToBeSpoken.get(i));
                tts.speak(textsToBeSpoken.get(i)._originalTxt, TextToSpeech.QUEUE_ADD, params);
                textsToBeSpoken.remove(index);
            }
        }
    }
}

in other thread somewere in the app i'll be inserting the objects to speak in textsToBeSpoken, and in other thread there'll be loop were it'll check for the size of the textsToBeSpoken if the size > 0, it'll call the speakUpSon() method.

until here everything works fine, but i get the following error messages


08-14 11:42:04.370: E/ActivityThread(4945): Activity com.PI.prototype.translator.Translator has leaked ServiceConnection android.speech.tts.TextToSpeech$Connection@b4669088 that was originally bound here


Solution

  • You have to call tts.shutdown() somewhere in your code, it is best in onStop() and call initTTS(); in onStart()