如何暂停android.speech.tts.TextToSpeech?

Jas*_*Jas 21 android text-to-speech

我正在用android TTS播放文字 - android.speech.tts.TextToSpeech

我用:TextToSpeech.speak说话和.stop停止.有没有办法暂停文本?

Aar*_*n C 23

TTS SDK没有我知道的任何暂停功能.但您可以使用synthesizeToFile()创建包含TTS输出的音频文件.然后,您将使用MediaPlayer对象播放,暂停和停止播放该文件.根据文本字符串的长度,可能需要更长的时间才能生成音频,因为该synthesizeToFile()函数必须先完成整个文件才能播放,但对于大多数应用程序来说,这种延迟应该是可以接受的.


小智 14

我使用了字符串拆分和使用playilence(),如下所示:

public void speakSpeech(String speech) {

    HashMap<String, String> myHash = new HashMap<String, String>();

    myHash.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "done");

    String[] splitspeech = speech.split("\\.");

    for (int i = 0; i < splitspeech.length; i++) {

        if (i == 0) { // Use for the first splited text to flush on audio stream

            textToSpeech.speak(splitspeech[i].toString().trim(),TextToSpeech.QUEUE_FLUSH, myHash);

        } else { // add the new test on previous then play the TTS

            textToSpeech.speak(splitspeech[i].toString().trim(), TextToSpeech.QUEUE_ADD,myHash);
        }

        textToSpeech.playSilence(750, TextToSpeech.QUEUE_ADD, null);
    }
}
Run Code Online (Sandbox Code Playgroud)


jro*_*oal 7

您可以通过添加最多三个句点(".")后跟单个空格""来使TTS在句子之间或任何您想要的位置暂停.下面的示例在开头有一个很长的停顿,并且在消息正文之前有一个停顿.我不确定那是你的意思.

    private final BroadcastReceiver SMScatcher = new BroadcastReceiver() {

    @Override
    public void onReceive(final Context context, final Intent intent) {
        if (intent.getAction().equals(
                "android.provider.Telephony.SMS_RECEIVED")) {
            // if(message starts with SMStretcher recognize BYTE)
            StringBuilder sb = new StringBuilder();

            /*
             * The SMS-Messages are 'hiding' within the extras of the
             * Intent.
             */
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                /* Get all messages contained in the Intent */
                Object[] pdusObj = (Object[]) bundle.get("pdus");
                SmsMessage[] messages = new SmsMessage[pdusObj.length];
                for (int i = 0; i < pdusObj.length; i++) {
                    messages[i] = SmsMessage
                            .createFromPdu((byte[]) pdusObj[i]);
                }
                /* Feed the StringBuilder with all Messages found. */
                for (SmsMessage currentMessage : messages) {
                    // periods are to pause
                    sb.append("... Message From: ");
                    /* Sender-Number */
                    sb.append(currentMessage.getDisplayOriginatingAddress());
                    sb.append(".. ");
                    /* Actual Message-Content */
                    sb.append(currentMessage.getDisplayMessageBody());
                }
                // Toast.makeText(application, sb.toString(),
                // Toast.LENGTH_LONG).show();
                if (mTtsReady) {
                    try {
                        mTts.speak(sb.toString(), TextToSpeech.QUEUE_ADD,
                                null);
                    } catch (Exception e) {
                        Toast.makeText(application, "TTS Not ready",
                                Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                    }
                }
            }

        }
    }
};
Run Code Online (Sandbox Code Playgroud)

如果在最后一个句点之后省略空格,它将(或可能)不按预期工作.


bra*_*all 5

如果没有暂停选项,您可以在想要延迟 TTS 引擎说话的持续时间内添加静音。例如,这当然必须是预定的“暂停”并且无助于包括暂停按钮的功能。

对于 API < 21 : public int playSilence (long durationInMs, int queueMode, HashMap params)

对于 > 21 : public int playSilentUtterance (long durationInMs, int queueMode, String utteranceId)

请记住使用TextToSpeech.QUEUE_ADD而不是TextToSpeech.QUEUE_FLUSH否则它会清除之前开始的语音。


rm *_*-rf 5

我使用了不同的方法。

  1. 将您的文本分成句子
  2. 把每一句一句一句说出来,并跟踪口述的句子
  3. 暂停将立即停止文本
  4. 简历将从最后一句的开头开始

科特林代码:

class VoiceService {

    private lateinit var textToSpeech: TextToSpeech    

    var sentenceCounter: Int = 0
    var myList: List<String> = ArrayList()

    fun resume() {
        sentenceCounter -= 1
        speakText()
    }

    fun pause() {
        textToSpeech.stop()
    }

    fun stop() {
        sentenceCounter = 0
        textToSpeech.stop()
    }

    fun speakText() {

        var myText = "This is some text to speak. This is more text to speak."

        myList =myText.split(".")

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        textToSpeech.speak(myList[sentenceCounter], TextToSpeech.QUEUE_FLUSH, null, utteranceId)
            sentenceCounter++
        } else {
            var map: HashMap<String, String> = LinkedHashMap<String, String>()
            map[TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID] = utteranceId
            textToSpeech.speak(myList[sentenceCounter], TextToSpeech.QUEUE_FLUSH, map)
            sentenceCounter++
        }
    }

    override fun onDone(p0: String?) {
        if (sentenceCounter < myList.size) {
            speakText()
        } else {
            speakNextText()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)