我正在开发一个应用程序,该应用程序将朗读文档中的文本,我想添加暂停和恢复功能,但是我在TTS中找不到任何pause()方法。是否有任何方法可以暂停?
我正在开发一个应用程序,该应用程序将朗读文档中的文本,我想添加暂停和恢复功能,但是我在TTS中找不到任何pause()方法。是否有任何方法可以暂停?
有一种方法可以暂停语音播报,只需调用TextToSpeech.playSilence()
,请参考下面的代码,来自这里。
它会播放一些实际文本以及一些间隔时间。
private void playScript()
{
Log.d(TAG, "started script");
// setup
// id to send back when saying the last phrase
// so the app can re-enable the "speak" button
HashMap<String, String> lastSpokenWord = new HashMap<String, String>();
lastSpokenWord.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,
LAST_SPOKEN);
// add earcon
final String EARCON_NAME = "[tone]";
tts.addEarcon(EARCON_NAME, "root.gast.playground", R.raw.tone);
// add prerecorded speech
final String CLOSING = "[Thank you]";
tts.addSpeech(CLOSING, "root.gast.playground",
R.raw.enjoytestapplication);
// pass in null to most of these because we do not want a callback to
// onDone
tts.playEarcon(EARCON_NAME, TextToSpeech.QUEUE_ADD, null);
tts.playSilence(1000, TextToSpeech.QUEUE_ADD, null);
tts.speak("Attention readers: Use the try button to experiment with"
+ " Text to Speech. Use the diagnostics button to see "
+ "detailed Text to Speech engine information.",
TextToSpeech.QUEUE_ADD, null);
tts.playSilence(500, TextToSpeech.QUEUE_ADD, null);
tts.speak(CLOSING, TextToSpeech.QUEUE_ADD, lastSpokenWord);
}
TextToSpeech类具有添加setOnUtteranceCompletedListener(或适用于api level 15+ setOnUtteranceProgressListener)的功能,这将允许您附加一个监听器,以便在TTS朗读完成时触发,然后您可以开始您的第二个朗读或暂停,或者任何您需要的操作。