gpt4 book ai didi

java - Android TTS 声音泄露服务连接和语音已弃用

转载 作者:行者123 更新时间:2023-12-02 04:40:12 30 4
gpt4 key购买 nike

我在类里面使用 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();
}

最佳答案

在调用 onInit 之前,您无法调用 speak,请将您的 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();
}

关于java - Android TTS 声音泄露服务连接和语音已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30280082/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com