如何知道TTS何时完成?

我正在Android上实现交互式语音应答程序。 我想知道如何确定什么时候tts.speak()函数已经完成通话,所以我可以调用我的语音识别器function。

Solutions Collecting From Web of "如何知道TTS何时完成?"

 public class TTSActivity extends Activity implements OnInitListener, OnUtteranceCompletedListener, ... { private TextToSpeech mTts; ........... private void speak(String text) { if(text != null) { 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"); mTts.speak(text, TextToSpeech.QUEUE_FLUSH, myHashAlarm); } } // Fired after TTS initialization public void onInit(int status) { if(status == TextToSpeech.SUCCESS) { mTts.setOnUtteranceCompletedListener(this); } } // It's callback public void onUtteranceCompleted(String utteranceId) { Log.i(TAG, utteranceId); //utteranceId == "SOME MESSAGE" } ........... } 

阅读 一个很好的教程

setOnUsermentCompletedListener从API级别15开始被弃用。相反,使用setOnUtteranceProgressListener 。

我发现了一个代码片段( 这里 ),使我很容易知道文本到语音的完成时间:

 @Override public void onInit(int status) { if (status == TextToSpeech.SUCCESS) { myTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() { @Override public void onDone(String utteranceId) { // Log.d("MainActivity", "TTS finished"); } @Override public void onError(String utteranceId) { } @Override public void onStart(String utteranceId) { } }); } else { Log.e("MainActivity", "Initilization Failed!"); } } 

http://www.codota.com/android/scenarios/52fcbd34da0ae25e0f855408/android.speech.tts.TextToSpeech?tag=dragonfly

我注意到,在使用TextToSpeech的时候,有人遇到了问题,要求你解决这个问题

如何知道TTS何时完成? 没有使用setOnUtteranceCompletedListener

 public void isTTSSpeaking(){ final Handler h =new Handler(); Runnable r = new Runnable() { public void run() { if (!tts.isSpeaking()) { onTTSSpeechFinished(); } h.postDelayed(this, 1000); } }; h.postDelayed(r, 1000); } 

做文字说话时使用SpeakableListener。

 private volatile boolean finished; private SpeakableListener listener = new SpeakableListener() { @Override public void markerReached(SpeakableEvent arg0) { // TODO Auto-generated method stub } @Override public void speakableCancelled(SpeakableEvent arg0) { // TODO Auto-generated method stub } @Override public void speakableEnded(SpeakableEvent arg0) { TestSpeaker.this.finished = true; } @Override public void speakablePaused(SpeakableEvent arg0) { // TODO Auto-generated method stub } @Override public void speakableResumed(SpeakableEvent arg0) { // TODO Auto-generated method stub } @Override public void speakableStarted(SpeakableEvent arg0) { TestSpeaker.this.finished = false; } @Override public void topOfQueue(SpeakableEvent arg0) { // TODO Auto-generated method stub } @Override public void wordStarted(SpeakableEvent arg0) { // TODO Auto-generated method stub } }; /** Speak the following string. */ public void sayIt(String str) { System.out.print(" " + str + " "); /* The the synthesizer to speak. */ synthesizer.speakPlainText(str, listener);