5

I'm trying to stop TextToSpeech, when back button pressed. But speech not stopping, even if I close my App. Only when I clear cache, speech stopping. How can I solve this? Please, help me to understand.

private boolean mShouldSpeak = true;
TextToSpeech tts;
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cat);

    tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS) {
                tts.setEngineByPackageName(enginePackageName);
                tts.setLanguage(Locale.getDefault());
                tts.setPitch(0);
                tts.setSpeechRate(1);
               speak();
            }
        }
    });
}
 private void speak() {

    if (mShouldSpeak == true)
    {
        tts.speak("Автор: " +getResources().getString(R.string.catAuthor), TextToSpeech.QUEUE_ADD, null);
        tts.playSilence(1000, TextToSpeech.QUEUE_ADD, null);
        tts.speak(getResources().getString(R.string.catName), TextToSpeech.QUEUE_ADD, null);
        tts.playSilence(1000, TextToSpeech.QUEUE_ADD, null);
        tts.speak(getResources().getString(R.string.catDesc), TextToSpeech.QUEUE_ADD, null);
        tts.playSilence(1000, TextToSpeech.QUEUE_ADD, null);
    }

}
 @Override
protected void onDestroy() {
    if (tts != null)
    {
        tts.stop();
        tts.shutdown();
    }
    super.onDestroy();
}
public void onBackPressed() {

    onDestroy();
    super.onBackPressed();

}
Valentin Michalak
  • 2,089
  • 1
  • 14
  • 27
Ghost46
  • 53
  • 6
  • Add logging in `onDestroy` - does it get to `tts.stop()` or is the tts object already null? – brandall Jan 22 '18 at 10:59
  • No, not already null. There is TextToSpeech@5089 in debug. – Ghost46 Jan 22 '18 at 15:29
  • 1
    Which TTS engine are you using in `setEngineByPackageName()` - have you tried another to make sure it's not an issue with the engine? Ideally, you really need to use the TTS API properly and fully, before you troubleshoot. If you don't it can cause issues. Add the correct bundle/map parameters, an utterance id and assign an `UtteranceProgressListener` so you can log the speech progress and see if it is attempted to be interrupted. – brandall Jan 22 '18 at 15:36
  • My gosh, I'm so stupid. You're absolutely right. I just started to learn Android and don't know very much. Not always examples, which are studying, give a comprehensive answer. You really helped, thanks a lot! <3 – Ghost46 Jan 22 '18 at 16:26
  • Glad you sorted it - we were all beginners once... Which one of my suggestions was the solution? I'll put it as an answer – brandall Jan 22 '18 at 16:28
  • 1
    It was a problem with the engine. Not quite knowing what it means setEngineByPackageName, I left the value from the example. Then changed to my package from the manifest. After your comment I tried to remove this option at all. And tts was to stop correctly. Thanks again, even though I do not fully understand the value setEngineByPackeageName, but I am ashamed of all this. And I will try to understand it) – Ghost46 Jan 22 '18 at 16:37
  • Added an answer with a further link in regarding Text to Speech engine debugging. – brandall Jan 22 '18 at 16:48

4 Answers4

1
 public void onPause(){
  if(tts !=null){
     tts.stop();
     tts.shutdown();
  }
  super.onPause();

}

Stop speak on activity pause

1

Further to the comments, you need to ensure:

tts.setEngineByPackageName(enginePackageName)

contains a valid package name of a Text to Speech engine that is installed on the device, such as com.google.android.tts or com.svox.pico.

To check information regarding installed engines, see my answer here

Not applying this parameter will bind the engine selected as the device default in the Text to Speech Settings.

brandall
  • 6,094
  • 4
  • 49
  • 103
0

Maybe you are creating numerous TextToSpeech objects? Then it might be that you are stopping the wrong tts.

Try a simple

if (tts == null) {
    tts = new TextToSpeech(...) {...}
}

to only initialise the object if it doesn't already exist.

Valentin Michalak
  • 2,089
  • 1
  • 14
  • 27
Algar
  • 5,734
  • 3
  • 34
  • 51
  • Tnx for advice, but it's only one TextToSpeech object. When I stop the process "speech synthesizer Google" in cache, TTS stops. How can I stop this process programmatically? – Ghost46 Jan 22 '18 at 09:28
  • Does your `tts.stop()` return a `TextToSpeech.ERROR` (-1) or a `TextToSpeech.SUCCESS` (0)? – Algar Jan 22 '18 at 09:32
  • Hello, tts.stop() returned SUCCESS(0) – Ghost46 Jan 22 '18 at 09:48
0

Modify like so, this usually works for me: I believe that you are using multiple TextToSpeech Objects, even if you do not think you are. It is a common problem.

@Override
    protected void onStop()
    {
        super.onStop();

        if(tts != null){
            tts.shutdown();
        }       
    }
Valentin Michalak
  • 2,089
  • 1
  • 14
  • 27
Cowboy Farnz
  • 329
  • 2
  • 11
  • I just... cant understand, sorry :c Why this happening? How to solve this? Cause it's still not working. What I doing wrong? – Ghost46 Jan 22 '18 at 15:25