Unable to detect completion of TTS (callback) android.

前端 未结 3 1145
情书的邮戳
情书的邮戳 2021-01-04 18:53

I am developing android application in which I am using text to speech conversion.What I need when I open my application run text to speech conversion. After completion of t

3条回答
  •  萌比男神i
    2021-01-04 19:25

    Here is some code from here that helps you be backward compatible so you don't have to target 15.

    private void setTtsListener()
        {
            final SpeechRecognizingAndSpeakingActivity callWithResult = this;
            if (Build.VERSION.SDK_INT >= 15)
            {
                int listenerResult = tts.setOnUtteranceProgressListener(new UtteranceProgressListener()
                {
                    @Override
                    public void onDone(String utteranceId)
                    {
                        callWithResult.onDone(utteranceId);
                    }
    
                    @Override
                    public void onError(String utteranceId)
                    {
                        callWithResult.onError(utteranceId);
                    }
    
                    @Override
                    public void onStart(String utteranceId)
                    {
                        callWithResult.onStart(utteranceId);
                    }
                });
                if (listenerResult != TextToSpeech.SUCCESS)
                {
                    Log.e(TAG, "failed to add utterance progress listener");
                }
            }
            else
            {
                int listenerResult = tts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener()
                {
                    @Override
                    public void onUtteranceCompleted(String utteranceId)
                    {
                        callWithResult.onDone(utteranceId);
                    }
                });
                if (listenerResult != TextToSpeech.SUCCESS)
                {
                    Log.e(TAG, "failed to add utterance completed listener");
                }
            }
        }
    

提交回复
热议问题