我正在开发使用文本到语音转换的android应用程序。打开应用程序时需要运行文本到语音的转换。完成此操作后,我想做一些事情。我的代码如下所示

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

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

最佳答案

如果您使用的是API级别15或更高版本,则可以使用以下命令在TextToSpeech引用上设置进度监听器

setOnUtteranceProgressListener(UtteranceProgressListener listener)

您将获得报告TTS进度的回调,包括完成时的回调。参见http://developer.android.com/reference/android/speech/tts/TextToSpeech.htmlhttp://developer.android.com/reference/android/speech/tts/UtteranceProgressListener.html

但是,我注意到您已经在使用不推荐使用的OnUtteranceCompletedListener。您是否收到onUtteranceCompleted()的回调?那也应该起作用。

关于android - 无法检测到TTS(回调)Android的完成。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11409177/

10-14 09:41
查看更多