【发布时间】:2012-06-18 16:33:21
【问题描述】:
我正在开发一个可以读出文档中文本的应用程序,我想添加暂停和恢复功能,但我在 TTS 中找不到任何 pause() 方法。有什么办法可以暂停..?
【问题讨论】:
我正在开发一个可以读出文档中文本的应用程序,我想添加暂停和恢复功能,但我在 TTS 中找不到任何 pause() 方法。有什么办法可以暂停..?
【问题讨论】:
有一种方法可以暂停。只需致电TextToSpeech.playSilence(),请参阅here 的以下代码。
它会说一些实际的文字以及一些沉默。
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 级别 15+ setOnUtteranceProgressListener),这将让您在 TTS 话语完成时附加一个监听器,然后您可以开始您的第二个话语,或者你的暂停,或者你需要的任何东西..
【讨论】: