Java – UtteranceProgressListener does not call functions

UtteranceProgressListener does not call functions… here is a solution to the problem.

UtteranceProgressListener does not call functions

I’m trying to make a voice-driven app, but I’m having a major problem.

No matter where I put the Speak method, my UtteranceProgressListener class doesn’t call any given method.

Here is my code :

Here is my OnCreate method:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = this;
    voiceBtn = (Button) findViewById(R.id.startListeningBtn);

voiceBtn.setEnabled(false);
    textToSpeech = new TextToSpeech(mContext,new botListener());

}    

This is the implementation of OnInitListner

public class botListener implements TextToSpeech.OnInitListener{
    @Override
    public void onInit(int i) {

if(i == TextToSpeech.SUCCESS)
        {
            int s = textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
            @Override
            public void onStart(String s) {
                Toast.makeText(getApplicationContext(),"Done Speaking",Toast.LENGTH_SHORT).show();
            }

@Override
            public void onDone(String s) {
                Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
            }

@Override
            public void onError(String s) {
                Toast.makeText(getApplicationContext(),"Done Speaking",Toast.LENGTH_SHORT).show();
            }
        });
        Log.d(TAG,String.valueOf(s));

int result = textToSpeech.setLanguage(Locale.ENGLISH);

if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){
                Log.e(TAG,"Language not supported");
                Intent installLanguage = new Intent(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installLanguage);
            }
            Log.d(TAG,"Started Voice Speaker");
        }
        else{
            Log.e(TAG,"initialization failed");
        }
    }
}

Now, when I press the button, the event that fires is:

public void initVoiceRecog(View v){
    Toast.makeText(mContext,"Clicked",Toast.LENGTH_SHORT).show();
    Speak("hello","1");
     does some other things here after that 

}

private void Speak(String text,String identifierID){

if(Build.VERSION.SDK_INT>21) {
        Bundle params = new Bundle();
        params.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,identifierID);
        textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, params, identifierID);
    }
    else{
     ttsMap is a HashMap
    ttsMap.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,identifierID);
    textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,ttsMap );
    }
}

My problem is that after saying hello, it doesn’t trigger the OnStart() or OnError() or OnDone() methods. Why is this?

I also tried using the deprecated setOnUtteranceListner() with the same result. It does not trigger any methods and does not display toasts.

Please advise the workaround or workaround.

The devices I tried are:

  • API 19 Micromax Canvas nitro
  • API 21 Samsung S4
  • API 23 (Marshmallow) Asus Zenfone
  • Solution

    I finally figured out why the callback didn’t work. It turns out that they are working and calling on a separate thread. Therefore, to perform normal function, call the function in “Activity.this.RunOnUiThread” and put it in the callback function.

    Related Problems and Solutions