无法检测到 TTS(回调)android 的完成. [英] Unable to detect completion of TTS (callback) android.

查看:33
本文介绍了无法检测到 TTS(回调)android 的完成.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 android 应用程序,其中使用文本到语音的转换.打开应用程序时我需要运行文本到语音的转换.完成后我想做一些事情.我的代码看起来像

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 this I want to do some thing.My code looks like

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();
       } 
   }
}

当我打开我的应用程序时,文本转语音工作正常.但是如何检测文本到语音是否完成.需要帮助.....谢谢.....

When I open my application text to speech working fine. But how to detect whether text to speech completed or not.Need help..... Thank you.....

推荐答案

如果您使用 API 级别 15 或更高版本,您可以使用

If you are using API level 15 or later you can set a progress listener on your TextToSpeech reference using

setOnUtteranceProgressListener(UtteranceProgressListener listener)

您将收到报告 TTS 进度的回调,包括完成时的回调.请参阅 http://developer.android.com/reference/android/speech/tts/TextToSpeech.htmlhttp://developer.android.com/reference/android/speech/tts/UtteranceProgressListener.html

You will get callbacks reporting the progress of the TTS, including a callback when it is finished. See http://developer.android.com/reference/android/speech/tts/TextToSpeech.html and http://developer.android.com/reference/android/speech/tts/UtteranceProgressListener.html

但是,我注意到您已经在使用已弃用的 OnUtteranceCompletedListener.你在 onUtteranceCompleted() 上收到回调了吗?这也应该有效.

However, I notice that you're already using the deprecated OnUtteranceCompletedListener. Are you getting the callbacks on onUtteranceCompleted()? That should also work.

这篇关于无法检测到 TTS(回调)android 的完成.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆