我采用了下面的代码,它工作正常,将 TTS 对象共享给其他 Activity :
package com.simekadam.blindguardian;
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
public class SpeechHelper implements OnInitListener {
private static TextToSpeech mTts;
private String text;
private static final SpeechHelper helper = new SpeechHelper();
public static SpeechHelper getInstance(){
return helper;
}
public void say(String text, Context context){
if(mTts == null){
this.text = text;
mTts = new TextToSpeech(context, (OnInitListener) helper);
}
else{
mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
public void stopTTS(){
if(mTts != null){
mTts.shutdown();
mTts.stop();
mTts = null;
}
}
}
但是,如果我离开 Activity 然后又回到它,我的应用程序就不再说话了。没有错误。它只是不再说话了。
有人能帮帮我吗?
谢谢!
我刚刚在 Eclipse 中的消息中看到以下消息:“正在向死线程上的处理程序发送消息”
我可以做什么来解决这个问题? =)
最佳答案
如果您使用应用程序上下文而不是当前 Activity ,则不会显示此错误。例如:
mTts = new TextToSpeech(context.getApplicationContext(), (OnInitListener) helper);
关于android - 共享 TTS 在 Activity 死了然后又活了之后没有工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9440955/