停止/关闭失败:未绑定到 TTS 引擎

and*_*ndy 5 android text-to-speech

我有一个使用 TTS 的 android 活动,但是当我退出它时,Logcat 中出现泄漏的服务连接错误。在泄漏的服务错误上方,我得到一个声明:

stop failed: not bound to TTS engine
shutdown failed: not bound to TTS engine
Run Code Online (Sandbox Code Playgroud)

我的 onStop 和 onDestroy 看起来像这样: 编辑代码:

@Override
public void onStop() {
    if (tts != null) {
    tts.stop();
    }
    super.onStop();
}

@Override
public void onDestroy() {
    if (tts != null) {
        tts.shutdown();
    }
    super.onDestroy();
}
Run Code Online (Sandbox Code Playgroud)

我在用户按下按钮时触发 TTS(TTS 工作正常,我只是想修复服务连接错误)

if (v == speakButton && ttscrashprotect == 1) {   
    String text = inputText.getText().toString();
    if (text != null && text.length() > 0) {
        tts.speak(text, TextToSpeech.QUEUE_ADD, null);
    }
Run Code Online (Sandbox Code Playgroud)

这是我的 onActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == MY_DATA_CHECK_CODE) {
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
            //sucess with TTS create it
            tts = new TextToSpeech(this, this);
        }
        else {
            //missing TTS so install it
            Intent installIntent = new Intent();
            installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installIntent);
        }
    }
Run Code Online (Sandbox Code Playgroud)

TTS 工作正常,我可以看到在 logcat 中调用了引擎,但由于某种原因,tts.stop()tts.shutdown()并没有绑定到同一个引擎。

EDIT2:我似乎不止一次连接到 TTS 引擎和服务。每次我希望 TTS 在活动中说出的文本时,都会与 TTS 引擎和 TTS 服务建立另一个连接。

文本更改时我的 TTS 代码如下所示:

if (v==nextButton && progressL1<100) {
    Video.setVisibility(View.INVISIBLE); //hide the video view

    progressL1= progressL1 + 10;
    //increase progress by 10 and then load the new files
    //load the TTS text data from Asset folder progressL1 txt file
    Advanced.this.loadDataFromAsset(progressL1);
    //try some tts stuff here
    Intent checkIntent = new Intent();
    checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
Run Code Online (Sandbox Code Playgroud)

TTS 是从v == speakButton上面的代码触发的。

Ale*_*eon 2

不要拨打tts.shutdown()两次,直接拨打onDestroy()就足够了。

@Override
public void onStop() {
    if (tts != null) {
        tts.stop();
    }
    super.onStop();
}

@Override
public void onDestroy() {
    if (tts != null) {
        tts.shutdown();
    }
    super.onDestroy();
}
Run Code Online (Sandbox Code Playgroud)