Android TTS onUtteranceCompleted callback isn't getting called

Justin Shidell picture Justin Shidell · Jan 11, 2011 · Viewed 18.2k times · Source

I am trying to get the Android TTS API to read my "utterance" and then call the onUtteranceCompleted() listener unsuccessfully. I've registered my TTS object and it returns SUCCESS, so I can't figure out for the life of me why my callback isn't getting called.

I've tried searching for help, but it seems others have difficulty with this too. Am I missing something simple?

Thanks for any help you can offer.

package com.test.mytts;

import java.util.HashMap;

import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
import android.widget.TextView;
import android.widget.Toast;

public class MyTTS extends Activity implements OnInitListener, OnUtteranceCompletedListener
{   
    TextView tv;
    private TextToSpeech _tts;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        tv = new TextView(this);

        tv.setText("MyTTS: ");

        super.onCreate(savedInstanceState);
        setContentView(tv);

        _tts = new TextToSpeech(this, this);
    }

    @Override
    public void onInit(int status) 
    {
        HashMap<String, String> myHashAlarm = new HashMap<String, String>();

        myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));
        myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "test");

        if (status == TextToSpeech.SUCCESS)
        {
            Toast.makeText(this, "Trying to speak...", Toast.LENGTH_SHORT).show();

            int result = _tts.setOnUtteranceCompletedListener(this);

            tv.append(String.valueOf(result));

            _tts.setSpeechRate((float) .5);

            _tts.speak("Testing one, two, three", TextToSpeech.QUEUE_ADD, myHashAlarm);
        }
        else
            Toast.makeText(this, "Failed to initialize TTS.", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onUtteranceCompleted(String utteranceId) 
    {
        Toast.makeText(this, "onUtteranceCompleted", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        _tts.shutdown();
    }
}

Answer

ekawas picture ekawas · Feb 22, 2011

I believe that unless you specify an utterance with an id, like:

map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceid);

your utterance completed method will not be called.

in this case, map is the Hashmap you pass to the engine when you speak.