javaandroidtext-to-speech

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


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.


Solution

  • 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);
    
        }
    
    }