0

I have been surfing the web (read google/ android developer document, read stackoverflow's similar questions, read book) and very closely followed the examples. The Text-to-Speech "speak" works great. But I just can't get OnUtteranceCompleted to be called.

It must be so simple that I am not seeing the answer. Please help! Here's my code after several interactions.

Or could someone be kind enough to provide a full source code (not snippets) of one that actually works to see if it works on my emulator/ actual device?

public class testActivity extends Activity implements OnInitListener, OnUtteranceCompletedListener {
...

protected void checkTtS() {
    Intent checkIntent = new Intent();
    checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(checkIntent, TTS_DATA_CHECK_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
....
   if (requestCode == TTS_DATA_CHECK_CODE) {
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
         // success, create the TTS instance
         mTts = new TextToSpeech(this, this);
 ....

 // Implements TextToSpeech.OnInitListener.
 public void onInit(int status) {

    // status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
    if (status == TextToSpeech.SUCCESS) {
        int result = mTts.setLanguage(Locale.FRANCE);
        result = mTts.setOnUtteranceCompletedListener(this);
        HashMap<String, String> params = new HashMap<String, String>();
        params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"stringId");
        mTts.speak("Text to Speak",TextToSpeech.QUEUE_FLUSH, params);
 ....

 public void onUtteranceCompleted(String uttId) {
    Toast.makeText(getBaseContext(), "onutterancecompleted", Toast.LENGTH_SHORT).show();
 }
mmmmm5
  • 151
  • 1
  • 1
  • 14

2 Answers2

0

have you checked the value of int result after the setOnUtteranceCompletedListener call.

if(result == TextToSpeech.ERROR)

then utterance listener was not set

AliDeo
  • 169
  • 6
  • result is equal to TextToSpeech.SUCCESS. So setOnUtteranceCompletedListener was successfully called. But, OnUtteranceCompleted still doesn't work – mmmmm5 Sep 27 '11 at 16:09
-1

Your code probably should look like this:

public void onUtteranceCompleted(String uttId) {
    if (uttId.equals("stringId")) {
        Toast.makeText(getBaseContext(), "onutterancecompleted", Toast.LENGTH_SHORT).show();
    }
 }

And you should also take a look at this article, there is a clear description of how onUtteranceCompleted works. Hope this helps.

Egor
  • 39,695
  • 10
  • 113
  • 130
  • Tried before and tried again. onUtteranceCompleted isn't called – mmmmm5 Sep 27 '11 at 15:07
  • 2
    After much trial and error, I managed to get it to work. Refer to [this](http://stackoverflow.com/questions/6645893/onutterancecompleted-does-not-get-called). And really make sure to put all code in //UI Changes. Otherwise, some features do not work, e.g. Toast – mmmmm5 Sep 28 '11 at 09:31