Android TextToSpeech: speak failed: not bound to TTS engine
Asked Answered
B

4

6

My TextToSpeech works fine on the first run, but it doesn't work after the application has been closed using "back" key and reopened. The error is TextToSpeech: speak failed: not bound to TTS engine and status in onInit is ERROR

I have a class to handle TTS:

public class VoiceGenerator {
    public TextToSpeech tts;
    private Context context;
    private String TAG = "Voice Generator";

    private static VoiceGenerator instance;

    private VoiceGenerator(Context context){
        this.context = context;
    }

    public static VoiceGenerator getInstance(Context context){
        if (instance == null) {
            instance = new VoiceGenerator(context);
        }

        return instance;
    }

    public void initializeTTS() {
        if (tts == null) {
            Log.d(TAG, "initialize tts");
            tts = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
                @Override
                public void onInit(int status) {
                    if (status != TextToSpeech.ERROR) {
                        Log.d(TAG, "initialize tts success");
                        tts.setLanguage(...);                           
                    }      
                }
            });    
        }
    }

    public void speak(){
        tts.speak(...)
    }

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

}

I get the instance of VoiceGenerator in onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);   
    voiceGenerator = VoiceGenerator.getInstance(this);
}

Initialize TTS in onStart:

@Override
protected void onStart(){
    super.onStart();
    voiceGenerator.initializeTTS();
}

And shut it down in onDestroy:

@Override
protected void onDestroy() {
super.onDestroy();
voiceGenerator.shutdown();    
}

Any ideas on what I am doing wrong?

Bohol answered 22/12, 2016 at 13:40 Comment(0)
C
6

You need to implement TextToSpeech.OnInitListener on the MainActivity. Here's a good example.

Code:

public class MainActivity extends ActionBarActivity  implements TextToSpeech.OnInitListener{

    private TextToSpeech engine;
    private EditText text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (EditText) findViewById(R.id.text);
        engine = new TextToSpeech(this, this);
    }



    public void speakText(View v) {

        String textContents = text.getText().toString();
        engine.speak(textContents, TextToSpeech.QUEUE_FLUSH, null, null);

    }

    @Override
    public void onInit(int i) {


        if (i == TextToSpeech.SUCCESS) {
            //Setting speech Language
            engine.setLanguage(Locale.ENGLISH);
            engine.setPitch(1);
        }
    }

}
Calistacalisthenics answered 4/10, 2017 at 1:35 Comment(1)
what if the initialization fails?Douse
G
1

Use onResume

@Override
protected void onResume() {
super.onResume();
myTTS = new TextToSpeech(this, this);
     }
}
Gasket answered 24/9, 2018 at 14:29 Comment(0)
H
0

Simple solution , not all devices support TextToSpeach , try with different devices, an

Hulsey answered 22/10, 2021 at 12:37 Comment(0)
G
-1

You can only let the engine speak, after onInit is done, so do following in onInit():

   if (status == TextToSpeech.SUCCESS) {
     tts.speak("Hello World", TextToSpeech.QUEUE_FLUSH, null);      

   }
Glomma answered 22/12, 2016 at 16:15 Comment(1)
The problem is that status is ERROR so this doesn't help.Bohol

© 2022 - 2024 — McMap. All rights reserved.