我在我的课堂上使用android tts只是说一条消息: 问题:我得到泄露的服务连接
public class WorkTimerNotification extends ActionBarActivity implements TextToSpeech.OnInitListener {
TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work_timer_notification);
//line1
tts = new TextToSpeech(this,this);
@Override
public void onInit(int status) {
//line2
tts.setLanguage(Locale.US);
//line3
tts.speak("Text to say aloud", TextToSpeech.QUEUE_ADD, null);
tts.shutdown();
}
@Override
protected void onDestroy() {
super.onDestroy();
tts.shutdown();
}
第1行我使用此logcat泄露了serviceConnection:
WorkTimerNotification has leaked ServiceConnection android.speech.tts.TextToSpeech$Connection@41aee290 that was originally bound here
android.app.ServiceConnectionLeaked
我按下这个,由于某种原因它仍然会回到哈希。我不知道为什么......
int speak(CharSequence, int, Bundle, String)
我也尝试过:但它也不起作用。
@Override
protected void onDestroy() {
//Close the Text to Speech Library
if(tts!= null) {
tts.stop();
ttsRest.shutdown();
Log.d(TAG, "TTS Destroyed");
}
super.onDestroy();
}
答案 0 :(得分:2)
在调用speak
之前,您无法致电onInit
,请将speak
代码移至onInit
public class WorkTimerNotification extends ActionBarActivity implements TextToSpeech.OnInitListener {
TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work_timer_notification);
//line1
}
@Override
public void onInit(int status) {
//line2
tts.setLanguage(Locale.US);
//line3
tts.speak("Text to say aloud", TextToSpeech.QUEUE_ADD, null);
}
@Override
protected void onStart()
{
super.onStart();
tts = new TextToSpeech(this,this);
}
@Override
protected void onStop()
{
super.onStop();
tts.shutdown();
}