I'm implementing an Interactive Voice Response application on Android. I would like to know how to determine when the tts.speak()
function has done talking so I can call my speech recognizer function.
Amazon Polly You can make the TTS pause between sentences, or anywhere you want by adding up to three periods (".") all followed by a single space " ".
Look under Accessibility > Text-to-speech output. Ensure you have "Google Text to Speech" selected and the correct language. Note that Speaking Email won't use Samsung or other vendor voices - so you need to enable the Google voices as your default TTS engine.
you can have this feature offline by doing as follows: Go to “Language and Input” in the Setting. Tap on "Download offline speech recognition" under the "Voice Search" Choose the language pack you want your Android device to recognize.
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
The setOnUtteranceCompletedListener is deprecated since API level 15. Instead, use setOnUtteranceProgressListener.
I found a code snippet (here) that made it really easy for me to know when text to speech is finished:
@Override public void onInit(int status) { if (status == TextToSpeech.SUCCESS) { myTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() { @Override public void onDone(String utteranceId) { // Log.d("MainActivity", "TTS finished"); } @Override public void onError(String utteranceId) { } @Override public void onStart(String utteranceId) { } }); } else { Log.e("MainActivity", "Initilization Failed!"); } }
http://www.codota.com/android/scenarios/52fcbd34da0ae25e0f855408/android.speech.tts.TextToSpeech?tag=dragonfly
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With