How to know when TTS is finished?
public class TTSActivity extends Activity implements OnInitListener, OnUtteranceCompletedListener, ... {
private TextToSpeech mTts;
...........
private void speak(String text) {
if(text != null) {
HashMap<String, String> myHashAlarm = new HashMap<String, String>();
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM));
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "SOME MESSAGE");
mTts.speak(text, TextToSpeech.QUEUE_FLUSH, myHashAlarm);
}
}
// Fired after TTS initialization
public void onInit(int status) {
if(status == TextToSpeech.SUCCESS) {
mTts.setOnUtteranceCompletedListener(this);
}
}
// It's callback
public void onUtteranceCompleted(String utteranceId) {
Log.i(TAG, utteranceId); //utteranceId == "SOME MESSAGE"
}
...........
}
Read A good tutorial
How to know when TextToSpeech is finished with speaking?
The problem is not in onUtteranceComplete
function , its most likely get called, but you're trying to print the output to System.out
, please use android.utils.Log("tag", "success")
instead and see what happens.
Also You need to set UtteranceID to get onUtteranceComplete
to work
before calling tts.speak
method. so to sum up:
TextToSpeech tts= new TextToSpeech(MainActivity.this, new OnInitListener() {
@Override
public void onInit(int status) {
tts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {
@Override
public void onUtteranceCompleted(final String utteranceId) {
System.out.println("Completed");
runOnUiThread(new Runnable() {
@Override
public void run() {
//UI changes
}
});
}
});
}
});
Map<String,String> ttsParams = new HashMap<String, String>();
ttsParams.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,
MainActivity.this.getPackageName());
mTts.speak(text, TextToSpeech.QUEUE_FLUSH, ttsParams);
And the exception you're getting has nothing to do with the onUtteranceComplete
method, if it is because of tts engine most likely you're not requesting to destroy your instance when finishing your activity.
To fix that add
@Override
onDestroy(){
super.onDestroy();
mTts.shutdown(); //mTts is your TextToSpeech Object
}
To your activity class
How can I Toast after Text to Speech finish speaking Android
You try to show a Toast
in a thread that is not the UI(main) thread.
You should change this
@Override
public void onUtteranceCompleted(String utteranceId) {
Log.i("CALLBACK", utteranceId); //utteranceId == "SOME MESSAGE"
Toast.makeText(getApplicationContext(),"Call Back",Toast.LENGTH_LONG).show();// I Cannot Toast here. Or do something more than Log
}
into this
@Override
public void onUtteranceCompleted(String utteranceId) {
Log.i("CALLBACK", utteranceId); //utteranceId == "SOME MESSAGE"
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),"Call Back",Toast.LENGTH_LONG).show();
}
});
}
That way your code is dispatched to the main thread where you are allowed to show Toast
s
Related Topics
Smoothscrolltopositionfromtop() Is Not Always Working Like It Should
How to Execute a Method by Clicking a Notification
Read Command Output Inside Su Process
Executing Multiple Asynctask's Parallely
How to Add a Gesture Detector to a View in Android
Very Large Soap Response - Android- Out of Memory Error
Adt Doesn't Create Default Hello World But Command Line Does
Android APK File: Re-Activate Older APK File in Google Play Store
Viewpager + Recyclerview Issue in Android
How to Remove the Top and Bottom Space on Textview of Android
Android - Multi-Line Linear Layout
Gradle Error:Execution Failed for Task ':App:Processdebuggoogleservices'
Load Large Images with Picasso and Custom Transform Object
Google Play Services V13 Error Meta-Data in Androidmanifest
Android 11 Action_Open_Document_Tree: Set Initial Uri to the Documents Folder