setOnUtteranceProgressListener not at all working for Text To Speech for API > 21
Asked Answered
D

2

8

I want setOnUtteranceProgressListener should notify a Toast after the speech is completed.It seems not working. I have used setOnUtteranceProgressListener and on the speak function i have mentioned the paramaters as follows..

    Bundle params = new Bundle();
    params.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, MainActivity.this.getPackageName());

I have given a "UniqueId" while calling speak function as follows.

    myTTS.speak(message,TextToSpeech.QUEUE_FLUSH,params,"UniqueId");

In My program after the text to speech engine finishes speaking it should run a Toast notifying that it has finished speaking.But the setOnUtteranceProgressListner seems not working.

    myTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
        @Override
        public void onStart(String utteranceId) {

        }

        @Override
        public void onDone(String utteranceId) {

            Toast.makeText(MainActivity.this,"Finished speaking.",Toast.LENGTH_LONG).show();
        }

        @Override
        public void onError(String utteranceId) {

        }
    });

The all Code is as follows..

    public class MainActivity extends AppCompatActivity {
String message;
private TextToSpeech myTTS;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if(myTTS.getEngines().size() == 0){
                Toast.makeText(MainActivity.this,"No Engines Installed",Toast.LENGTH_LONG).show();
            }else{
                myTTS.setLanguage(Locale.US);

                if (status == TextToSpeech.SUCCESS){
                    //Toast.makeText(MainActivity.this,"Status working.",Toast.LENGTH_LONG).show();
                    message = "How may i help you.";
                }

            }
        }
    });

    myTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
        @Override
        public void onStart(String utteranceId) {

        }

        @Override
        public void onDone(String utteranceId) {

            Toast.makeText(MainActivity.this,"onDone working.",Toast.LENGTH_LONG).show();
        }

        @Override
        public void onError(String utteranceId) {

        }
    });
}

Please give a solution for this.

Defrost answered 8/9, 2018 at 7:53 Comment(0)
M
14

The main problems are:

1) Setting the progress listener before the tts is initialized.

2) Trying to make a Toast from a background thread.

I also have some other suggested changes but they are not required:

public class MainActivity extends AppCompatActivity {
    String message = "How may I help you?";
    String mostRecentUtteranceID;
    private TextToSpeech myTTS;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if(myTTS.getEngines().size() == 0){
                    Toast.makeText(MainActivity.this,"No Engines Installed",Toast.LENGTH_LONG).show();
                }else{
                    if (status == TextToSpeech.SUCCESS){
                        ttsInitialized();
                    }
                }
            }
        });
    }

    private void ttsInitialized() {

        // *** set UtteranceProgressListener AFTER tts is initialized ***
        myTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
            @Override
            public void onStart(String utteranceId) {

            }

            @Override
            // this method will always called from a background thread.
            public void onDone(String utteranceId) {

                // only respond to the most recent utterance
                if (!utteranceId.equals(mostRecentUtteranceID)) { 
                    Log.i("XXX", "onDone() blocked: utterance ID mismatch.");
                    return; 
                } // else continue...

                boolean wasCalledFromBackgroundThread = (Thread.currentThread().getId() != 1);
                Log.i("XXX", "was onDone() called on a background thread? : " + wasCalledFromBackgroundThread);

                Log.i("XXX", "onDone working.");

                // for demonstration only... avoid references to 
                // MainActivity (unless you use a WeakReference)
                // inside the onDone() method, as it
                // can cause a memory leak.
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // *** toast will not work if called from a background thread ***
                        Toast.makeText(MainActivity.this,"onDone working.",Toast.LENGTH_LONG).show();
                    }
                });
            }

            @Override
            public void onError(String utteranceId) {

            }
        });

        // set Language
        myTTS.setLanguage(Locale.US);

        // set unique utterance ID for each utterance
        mostRecentUtteranceID = (new Random().nextInt() % 9999999) + ""; // "" is String force

        // set params
        // *** this method will work for more devices: API 19+ ***
        HashMap<String, String> params = new HashMap<>();
        params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, mostRecentUtteranceID);

        myTTS.speak(message,TextToSpeech.QUEUE_FLUSH,params);

    }

}
Mulry answered 10/9, 2018 at 23:5 Comment(7)
Can i have your gmail id.Defrost
how can i run the speak() method without button that is directly from the main activity.Defrost
No, but you can always ask another question. Glad I could/can help.Mulry
Please look into this problem. #52217661Defrost
is it possible to invoke speech recognition from a AsyncTask.Defrost
Please look into this problem. #52380357Defrost
in my case I didn't provide the HashMap parameter in the tts.speak("Your message to speak",TextToSpeech.QUEUE_FLUSH, map);Kalliekallista
P
10

If you want to add the call back OnUtteranceProgressListener you have to implement the speak method like this:

myTTS.speak(message,TextToSpeech.QUEUE_FLUSH, null , TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID);

Then it will call the methods that you've already implemented (onStart, onDone, etc)

Pseudoscope answered 15/4, 2022 at 17:28 Comment(2)
adding utterance id did the trick for me. Not sure why though(tested on android 10)Neurologist
This was the solution for jetpack compose kotlin-based app. Thanks!Troytroyer

© 2022 - 2024 — McMap. All rights reserved.