I've tried to call some Methods on start and end of text to speech so I used the setOnUtteranceProgressListener
but it doesn't work/get called.
What am I doing wrong?
Here the needed code:
Class:
public class SpeechRecognizerActivity extends Activity implements TextToSpeech.OnInitListener
Init Method:
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
String language = Locale.getDefault().getLanguage();
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
Method where speaking starts:
private void speakOut(String text) {
setTtsListener();
tts.setPitch(1.5f);
tts.setSpeechRate(1.5f);
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
The Listener (Method gets called in speakOut): None of those logs get displayed in Logcat.
private void setTtsListener() {
if (Build.VERSION.SDK_INT >= 15)
{
int listenerResult = tts.setOnUtteranceProgressListener(new UtteranceProgressListener()
{
@Override
public void onDone(String utteranceId)
{
Log.d(TAG,"progress on Done " + utteranceId);
}
@Override
public void onError(String utteranceId)
{
Log.d(TAG,"progress on Error " + utteranceId);
}
@Override
public void onStart(String utteranceId)
{
Log.d(TAG,"progress on Start " + utteranceId);
}
});
if (listenerResult != TextToSpeech.SUCCESS)
{
Log.e(TAG, "failed to add utterance progress listener");
}
}
else
{
int listenerResult = tts.setOnUtteranceCompletedListener(new TextToSpeech.OnUtteranceCompletedListener()
{
@Override
public void onUtteranceCompleted(String utteranceId)
{
Log.d(TAG,"progress on Completed " + utteranceId);
}
});
if (listenerResult != TextToSpeech.SUCCESS)
{
Log.e(TAG, "failed to add utterance completed listener");
}
}
}
A little workaround I found was this:
boolean speakingEnd = tts.isSpeaking();
do {
speakingEnd = tts.isSpeaking();
} while ((speakingEnd));
Log.d(TAG,"talking stopped");
I've added it a the end of the speakOut
method, but this solution isn't very good.
Working with the listeners would be perfect.
So what am I doing wrong?
When you call tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
You need to pass it a map with the KEY_PARAM_UTTERANCE_ID
HashMap<String, String> map = new HashMap<String, String>();
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"messageID");
tts.speak(text, TextToSpeech.QUEUE_FLUSH, map);
This lets TextToSpeech know to use your utterance listener for that text
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