I have been able to run TTS from an activity but when I try to execute the same code from a service, it's giving me message that TTS engine is initialised but not speaking anything.
Has anybody encountered the same issue anytime?
public void onCreate() {
super.onCreate();
tts = new TextToSpeech(this, this //TextToSpeech.OnInitListener);
timer.scheduleAtFixedRate( new TimerTask()
{ // In timer
public void run() {
//On some condition
tts.speak("thank you", TextToSpeech.QUEUE_ADD, null);
}, 0, 60000);
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
Toast.makeText(BackgroundProcessforTimecheck.this,
"Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
}
else if (status == TextToSpeech.ERROR) {
Toast.makeText(BackgroundProcessforTimecheck.this,
"Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
}
}
I had the same problem and solved it this way: Instantiate TextToSpeech Object in a separate Class (in my case I furthermore use a Factory to check if the Android Version is at least Donut) and reuse it (see method init(Context context)). Please notice that the onInit(int status) is far from being ready to release.
Service:
Factory:
Implementation:
Did you installed the TTS engine in your device. I too faced the same problem with the same message. But later I found that I have to install the TTS engine to my real device also and it is available in the play store.