The Java utteranceprogresslistener does not call the function

I'm trying to make a voice driven application, but I have a major problem

No matter where I put the speak method, my butteranceprogresslistener class will not call any given method

This is my code:

This 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 one rror(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 triggered event 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 question is, after saying hello, it doesn't trigger onstart() or onerror() or ondone() methods. Why?

I also try to use the outdated setonutterancelister (), and the result is the same. It won't trigger any methods and toasts won't appear

Please advise the solution or solution

The devices I have tried are:

>API 19 micromax canvas nitro > API 21 Samsung S4 > API 23 (Marshmellow) ASUS zenfone

resolvent:

I finally figured out what the callbacks didn't work. They were working and calling on separate threads. Therefore, to perform routine functions, call the function in "Activity.this.RunOnUiThread" and place it in the callback function.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>