Reputation: 17
My TTS in service is not stopping and speak two times. In logcat the error is "stop failed: not bound to TTS engine" I am stuck here that why TTS is not stoping. What wil I do here to stop it when speaked. Here is my code
public class Speaker extends Service implements TextToSpeech.OnInitListener {
public static TextToSpeech mtts;
String speech = "";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
}
@Override
public void onStart(Intent intent, int startid) {
speech=intent.getExtras().getString("speech");
mtts = new TextToSpeech(getApplicationContext(), this);
}
@Override
public void onDestroy() {
if (mtts != null) {
Log.e("Destroy", "Service");
mtts.stop();
mtts.shutdown();
}
super.onDestroy();
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS)
mtts.speak(speech, TextToSpeech.QUEUE_ADD, null);
}
Log.e("onInit ends", "Service");
}
}
}
Upvotes: 0
Views: 859
Reputation: 10623
One reason is that when a phone call is incoming then at this time your broadcast receiver would be calling this activity again and again unless you pick the phone If you want to stop this service after make it speak for once. Code for stopping the service.
Intent in = new Intent(this, Speaker.class);
stopService(in);
Use this code inside onDestroy() or in onStart() method. Why you are using the service for TTS?, you can use any activity or class etc.
Upvotes: 1