2

My code:

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.UtteranceProgressListener;
import android.widget.Toast;

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
@SuppressLint("NewApi")
public class MainActivity extends Activity {
    public TextToSpeech tts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tts = new TextToSpeech(this, new ttsInitListener());
    }
    @SuppressLint("TrulyRandom") class ttsInitListener implements OnInitListener {

        @SuppressWarnings("deprecation")
        @Override
        public void onInit(int status) {

            if (status == TextToSpeech.SUCCESS) {
                tts.setLanguage(Locale.getDefault());
                if (!tts.isSpeaking()) {
                    SecureRandom random = new SecureRandom();
                    String aa= new BigInteger(130, random).toString(32);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, aa);
                    tts.speak("Hello Hello HEllo HEllo HEllo",
                            TextToSpeech.QUEUE_FLUSH, map);
                    tts.setOnUtteranceProgressListener(new ttsUtteranceListener());
                }

            } else {
                tts = null;
                Toast.makeText(getApplicationContext(),
                        "Failed to initialize TTS engine.", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    class ttsUtteranceListener extends UtteranceProgressListener {

        @Override
        public void onDone(String utteranceId) {

            Toast.makeText(getApplicationContext(), "onDone", Toast.LENGTH_LONG).show();

        }

        @Override
        public void onError(String utteranceId) {
            Toast.makeText(getApplicationContext(), "onError", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onStart(String utteranceId) {
            Toast.makeText(getApplicationContext(), "onStart", Toast.LENGTH_LONG).show();
        }
    }
}

When speak

tts.speak("Hello Hello HEllo HEllo HEllo",
                                TextToSpeech.QUEUE_FLUSH, map);

Finish then should call onDone(String utteranceId) method of ttsUtteranceListener class. but ttsUtteranceListener is not always reached.

Why is this class not reached?

jopasserat
  • 5,721
  • 4
  • 31
  • 50
  • Did you debug what's created in `String aa= new BigInteger(130, random).toString(32);`? Try with static param once. – MysticMagicϡ Dec 17 '14 at 07:10
  • Before, i use map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "abc"); –  Dec 17 '14 at 07:19
  • Try putting `map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "hello");` As your text doesn't contain abc but hello. – MysticMagicϡ Dec 17 '14 at 07:20
  • Because UtteranceProgressListener is for listening to progress of utterance of particular text. It will be fired if *that* text gets spoken. – MysticMagicϡ Dec 17 '14 at 07:22
  • SecureRandom random = new SecureRandom(); String aa= new BigInteger(5, random).toString(32); String abc="abc"+aa; Toast.makeText(_mContext, "value:"+abc, Toast.LENGTH_LONG).show(); HashMap map = new HashMap(); map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, abc); –  Dec 17 '14 at 07:30
  • not call any method of ttsUtteranceListener class. Beacous not show any Toast message. –  Dec 17 '14 at 07:43
  • HashMap map = new HashMap(); map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "hello"); tts.speak("Hello Hello HEllo HEllo HEllo", TextToSpeech.QUEUE_FLUSH, map); tts.setOnUtteranceProgressListener(new ttsUtteranceListener()); –  Dec 17 '14 at 07:49

1 Answers1

3

You are using this:

String aa= new BigInteger(130, random).toString(32);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, aa);  //aa can be anything random, not necessary a word contained in your text

UtteranceProgressListener is a Listener for events relating to the progress of an utterance through the synthesis queue.

onDone : Called when an utterance has successfully completed processing.

You should try using this:

HashMap<String, String> map = new HashMap<String, String>();
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "Hello");
tts.speak("Hello Hello HEllo HEllo HEllo", TextToSpeech.QUEUE_FLUSH, map);
tts.setOnUtteranceProgressListener(new ttsUtteranceListener());

Then it will be fired when utterance of Hello is completed successfully.

Hope it helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124