简体   繁体   中英

Speak failed, not bound to TTS Engine (on a Service)

So I have a Service that is able to speak. I also have an Activity that enables/disables the Service from speaking.

The thing is, when I Start the Service after destroying it the TTS disconnects from the Engine, how do I reconnect it?

@Override
public void onStart(Intent intent, int startid) {

    if (tts == null) {
        tts = new TextToSpeech(this, this);
            }
    }

and the onInit

@Override
public void onInit(int status) {

    this.status = status;
    if (this.status == TextToSpeech.SUCCESS) {

        if (tts == null) {
            tts = new TextToSpeech(this, this);
            tts.setSpeechRate(0.8f);
        }
        int result = tts.setLanguage(Locale.UK);

        if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS", "This Language is not supported");
            Intent installIntent = new Intent();
            installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installIntent);
        }
    }

}

onDestroy

@Override
public void onDestroy() {
    if (tts != null) {
        tts.stop();
        tts.shutdown();
        Log.d("TAG", "TTS Destroyed");
    }
    super.onDestroy();
}

I would suggest adding some logging into your code to see where it is failing.

Firstly, try putting tts = null; after tts.shutdown(); in onDestroy()

If that doesn't work, check the status onInit . At the moment your code handles success, but you appear to do nothing if you get a fail message message.

Put logging in the OnStart and OnInit to check you are correctly calling them - it may be an error somewhere in your code that you are not detailing above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM