无法检测TTS(回调)机器人完成。(Unable to detect completion of T

2019-06-26 04:02发布

我正在开发中,我使用文本到语音conversion.What我需要当我睁开应用程序运行文本到语音转换android应用。 这完成后我想做一些thing.My代码看起来像

public class Mainactivity extends Activity implements OnInitListener, OnUtteranceCompletedListener{
    private static int REQ_CODE = 1;
    private TextToSpeech tts = null;
    private boolean ttsIsInit = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    startTextToSpeech();
    }

    private void startTextToSpeech() {
        Intent intent = new Intent(Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(intent, REQ_CODE);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQ_CODE) {
            if (resultCode == Engine.CHECK_VOICE_DATA_PASS) {
                tts = new TextToSpeech(this, this); 
            } 
            else {
                Intent installVoice = new Intent(Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installVoice);
            }
        }
    }

        public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS) {
                ttsIsInit = true;
                int result = tts.setOnUtteranceCompletedListener(this);
                if (tts.isLanguageAvailable(Locale.ENGLISH) >= 0)
                    tts.setLanguage(Locale.ENGLISH);
                tts.setPitch(5.0f);
                tts.setSpeechRate(1.0f);

                 HashMap<String, String> myHashAlarm = new HashMap<String, String>();
                  myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM));
                  myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "SOME MESSAGE");
                  tts.speak("hi how are you?", TextToSpeech.QUEUE_FLUSH, myHashAlarm);
             }
        }

   @Override
   public void onDestroy() {
      if (tts != null) {
        tts.stop();
        tts.shutdown();
      }
        super.onDestroy();
     }

   @Override
   public void onUtteranceCompleted(String uttId) {
       Toast.makeText(Mainactivity.this,"done", Toast.LENGTH_LONG).show();
       if (uttId.equalsIgnoreCase("done")) {
           Toast.makeText(Mainactivity.this,"inside done", Toast.LENGTH_LONG).show();
       } 
   }
}

当我打开我的应用程序文本到语音转换工作的罚款。 但是,如何检测文本到语音完成或not.Need帮助是否.....谢谢.....

Answer 1:

如果您使用的API级别15或更高版本,你可以设置你的进展听众TextToSpeech使用参考

setOnUtteranceProgressListener(UtteranceProgressListener listener)

你会得到回调报告TTS的进展,包括一个回调,当它完成。 见http://developer.android.com/reference/android/speech/tts/TextToSpeech.html和http://developer.android.com/reference/android/speech/tts/UtteranceProgressListener.html

然而,我注意到,你已经在使用过时的OnUtteranceCompletedListener 。 你好吗回调onUtteranceCompleted() 这也应该工作。



Answer 2:

调用TTS对象的OnInit函数里面的setOnUtteranceCompletedListener。

如果你想对onUtteranceCompleted函数的调用用户界面的任何更改,添加代码runOnUIThread方法中。

而且千万记住,同时调用说话()函数中添加HashMap的参数值的方式

例如:

TextToSpeech tts= new TextToSpeech(context, new OnInitListener() {

 @Override
 public void onInit(int status) {

    mTts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {

        @Override
        public void onUtteranceCompleted(String utteranceId) {

            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                //UI changes
                }
            });
        }
    });

 }
});


HashMap<String, String> params = new HashMap<String, String>();

params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"stringId");

tts.speak("Text to Speak",TextToSpeech.QUEUE_FLUSH, params);


Answer 3:

下面是一些代码在这里 ,可以帮助你是向后兼容的,所以你不必目标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");
            }
        }
    }


文章来源: Unable to detect completion of TTS (callback) android.