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?
You can only let the engine speak, after onInit is done, so do following in onInit():