如何知道TTS什么时候结束?

时间:2011-01-11 13:59:06

标签: android text-to-speech

我正在Android上实现交互式语音应答应用程序。我想知道如何确定tts.speak()函数何时完成说话,以便我可以调用我的语音识别器功能。

6 个答案:

答案 0 :(得分:43)

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"
   }
...........
}

阅读 A good tutorial

答案 1 :(得分:16)

自API级别15以来,不推荐使用setOnUtteranceCompletedListener。而是使用setOnUtteranceProgressListener

我找到了一个代码段(here),这让我很容易知道文本到语音的完成时间:

@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

答案 2 :(得分:3)

我注意到有些人在使用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);
}

答案 3 :(得分:1)

尝试以下代码,该代码显示TTS完成后的祝酒词。用自己的动作替换吐司。

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener{


private boolean initialized;
private String queuedText;
private String TAG = "TTS";
private TextToSpeech tts;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    tts = new TextToSpeech(this /* context */, this /* listener */);
    tts.setOnUtteranceProgressListener(mProgressListener);


    speak("hello world");

}




public void speak(String text) {

    if (!initialized) {
        queuedText = text;
        return;
    }
    queuedText = null;

    setTtsListener(); // no longer creates a new UtteranceProgressListener each time
    HashMap<String, String> map = new HashMap<String, String>();
    map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "MessageId");
    tts.speak(text, TextToSpeech.QUEUE_ADD, map);
}


private void setTtsListener() {

}





@Override
public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
        initialized = true;
        tts.setLanguage(Locale.ENGLISH);

        if (queuedText != null) {
            speak(queuedText);
        }
    }
}



private abstract class runnable implements Runnable {
}




private UtteranceProgressListener mProgressListener = new UtteranceProgressListener() {
    @Override
    public void onStart(String utteranceId) {
    } // Do nothing

    @Override
    public void onError(String utteranceId) {
    } // Do nothing.

    @Override
    public void onDone(String utteranceId) {

        new Thread()
        {
            public void run()
            {
                MainActivity.this.runOnUiThread(new runnable()
                {
                    public void run()
                    {

                        Toast.makeText(getBaseContext(), "TTS Completed", Toast.LENGTH_SHORT).show();

                    }
                });
            }
        }.start();

    }
}; }

答案 4 :(得分:1)

要知道TTS何时完成,您必须调用setOnUtteranceProgressListener,它具有3种回调方法onStartonDoneonError 然后在语音方法中包含一个话语ID

代码段

textToSpeech=new TextToSpeech(this, new TextToSpeech.OnInitListener() {
    @Override
    public void onInit(int status) {
        if (status==TextToSpeech.SUCCESS){
            int result=textToSpeech.setLanguage(Locale.ENGLISH);

            if (result==TextToSpeech.LANG_MISSING_DATA||result==TextToSpeech.LANG_NOT_SUPPORTED){
                Log.i("TextToSpeech","Language Not Supported");
            }

            textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
                @Override
                public void onStart(String utteranceId) {
                    Log.i("TextToSpeech","On Start");
                }

                @Override
                public void onDone(String utteranceId) {
                    Log.i("TextToSpeech","On Done");
                }

                @Override
                public void onError(String utteranceId) {
                    Log.i("TextToSpeech","On Error");
                }
            });

        }else {
            Log.i("TextToSpeech","Initialization Failed");
        }
    }
});


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,null,TextToSpeech.ACTION_TTS_QUEUE_PROCESSING_COMPLETED);
    }

答案 5 :(得分:-1)

在发言时使用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);