Java Android TTS声音泄漏服务连接和语音不推荐

Java Android TTS声音泄漏服务连接和语音不推荐,java,android,text-to-speech,speech,Java,Android,Text To Speech,Speech,我在课堂上使用android tts只是想说一句这样的话: 问题:我得到泄漏的服务连接 public class WorkTimerNotification extends ActionBarActivity implements TextToSpeech.OnInitListener { TextToSpeech tts; @Override protected void onCreate(Bundle savedInstanceState) { super.onC

我在课堂上使用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的连接:

 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();
}

您不能调用
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();
}

自API 21以来,第3行已被弃用,请使用
public int speak(CharSequence text,int queueMode,Bundle params,String outtanceid)
。此外,自上次AppCompat更新以来,
ActionBarActivity
已被弃用。对于您关于无声音的问题,我希望更高级的用户可以为您解决。谢谢。我尝试使用public int speak(CharSequence text、int queueMode、Bundle params、String outtanceid),但它只是恢复到原来的状态。我不明白为什么它一直在返回。当你在onInit中调用shutdown时,你可能听不到任何声音,因为speak是异步的。我想,我应该把它放在哪里?把它放在冰箱里?我知道它只会破坏声音。我还得到一个错误,它不能在调用tts.shutdown作为错误的地方结束活动。我在顶部有这个错误,我调用private。假设我的意思是它在公共类{}内。我仍然会犯同样的错误。