我正在构建一个应用程序,其中有tts部分,在主要活动中我正在创建一个tts实例,如
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;
}
}
我从onCreate()方法调用initTTS()方法;
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");
}
});
}
});
}
还有一个名为
的函数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);
}
}
}
}
在其他线程中,我会在应用程序中插入要在textsToBeSpoken中发言的对象,而在其他线程中,如果大小&gt;它将检查textToBeSpoken的大小。 0,它将调用speakUpSon()方法。
直到这里一切正常,但我收到以下错误消息
08-14 11:42:04.370:E / ActivityThread(4945):活动com.PI.prototype.translator.Translator已泄露最初绑定的ServiceConnection android.speech.tts.TextToSpeech$Connection@b4669088
答案 0 :(得分:1)
你必须在你的代码中的某个地方调用tts.shutdown(),最好在onStop()中调用initTTS();在onStart()