How to pause android.speech.tts.TextToSpeech?

前端 未结 10 985
不知归路
不知归路 2020-12-01 03:47

I\'m playing text with android TTS - android.speech.tts.TextToSpeech

I use: TextToSpeech.speak to speak and .stop to stop. Is

10条回答
  •  渐次进展
    2020-12-01 04:15

    You can make the TTS pause between sentences, or anywhere you want by adding up to three periods (".") all followed by a single space " ". The example below has a long pause at the beginning, and again before the message body. I'm not sure that is what you are after though.

        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();
                        }
                    }
                }
    
            }
        }
    };
    

    If you omit the space after the last period it will (or may) not work as expected.

提交回复
热议问题