How to know when TTS is finished?

前端 未结 6 1056
臣服心动
臣服心动 2020-11-28 08:06

I\'m implementing an Interactive Voice Response application on Android. I would like to know how to determine when the tts.speak() function has done talking so

6条回答
  •  我在风中等你
    2020-11-28 08:49

    To know when TTS is finished you have to call the setOnUtteranceProgressListener which has 3 call back methods onStart,onDone and onError then include a Utterance Id to the speak method

    Code Snippet

    textToSpeech=new TextToSpeech(this, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status==TextToSpeech.SUCCESS){
                int result=textToSpeech.setLanguage(Locale.ENGLISH);
    
                if (result==TextToSpeech.LANG_MISSING_DATA||result==TextToSpeech.LANG_NOT_SUPPORTED){
                    Log.i("TextToSpeech","Language Not Supported");
                }
    
                textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
                    @Override
                    public void onStart(String utteranceId) {
                        Log.i("TextToSpeech","On Start");
                    }
    
                    @Override
                    public void onDone(String utteranceId) {
                        Log.i("TextToSpeech","On Done");
                    }
    
                    @Override
                    public void onError(String utteranceId) {
                        Log.i("TextToSpeech","On Error");
                    }
                });
    
            }else {
                Log.i("TextToSpeech","Initialization Failed");
            }
        }
    });
    
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,null,TextToSpeech.ACTION_TTS_QUEUE_PROCESSING_COMPLETED);
        }
    

提交回复
热议问题