我正在构建一个包含 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 中插入要说话的对象,而在其他线程中,如果大小> 0,它将检查 textsToBeSpoken 的大小,它将调用speakUpSon() 方法。
直到这里一切正常,但我收到以下错误消息
08-14 11:42:04.370: E/ActivityThread(4945): Activity com.PI.prototype.translator.Translator 已泄露 ServiceConnection android.speech.tts.TextToSpeech$Connection@b4669088 最初绑定在这里