How to pause android.speech.tts.TextToSpeech?

前端 未结 10 1010
不知归路
不知归路 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:19

    This solution is not perfect, but an alternative to @Aaron C's solution may be to create a custom text to speech class like the below. This solution may work well enough if your text is relatively short and spoken words per minute is accurate enough for the language you are using.

    private class CustomTextToSpeech extends TextToSpeech {
        private static final double WORDS_PER_MS = (double)190/60/1000;
    
        long startTimestamp = 0;
        long pauseTimestamp = 0;
    
        private Handler handler;
        private Runnable speakRunnable;
    
        StringBuilder textToSpeechBuilder;
    
        private boolean isPaused = false;
    
        public CustomTextToSpeech(Context context, OnInitListener initListener){
            super(context, initListener);
    
            setOnUtteranceProgressListener(new UtteranceProgressListener() {
                @Override
                public void onDone(String arg0) {
                    Log.d(TAG, "tts done. " + arg0);
                    startTimestamp = 0;
                    pauseTimestamp = 0;
                    handler.postDelayed(speakRunnable, TTS_INTERVAL_MS);
                }
    
                @Override
                public void onError(String arg0) {
                    Log.e(TAG, "tts error. " + arg0);
                }
    
                @Override
                public void onStart(String arg0) {
                    Log.d(TAG, "tts start. " + arg0);
                    setStartTimestamp(System.currentTimeMillis());
                }
            });
    
            handler = new Handler();
    
            speakRunnable = new Runnable() {
                @Override
                public void run() {
                    speak();
                }
            };
    
            textToSpeechBuilder = new StringBuilder(getResources().getString(R.string.talkback_tips));
        }
    
        public void setStartTimestamp(long timestamp) {
            startTimestamp = timestamp;
        }
        public void setPauseTimestamp(long timestamp) {
            pauseTimestamp = timestamp;
        }
    
        public boolean isPaused(){
            return (startTimestamp > 0 && pauseTimestamp > 0);
        }
    
        public void resume(){
            if(handler != null && isPaused){
                if(startTimestamp > 0 && pauseTimestamp > 0){
                    handler.postDelayed(speakRunnable, TTS_SETUP_TIME_MS);
                } else {
                    handler.postDelayed(speakRunnable, TTS_INTERVAL_MS);
                }
            }
    
            isPaused = false;
        }
    
        public void pause(){
            isPaused = true;
    
            if (handler != null) {
                handler.removeCallbacks(speakRunnable);
                handler.removeMessages(1);
            }
    
            if(isSpeaking()){
                setPauseTimestamp(System.currentTimeMillis());
            }
    
            stop();
        }
    
        public void utter(){
            if(handler != null){
                handler.postDelayed(speakRunnable, TTS_INTERVAL_MS);
            }
        }
    
        public void speak(){
            Log.d(TAG, "textToSpeechBuilder: " + textToSpeechBuilder.toString());
            if(isPaused()){
                String[] words = textToSpeechBuilder.toString().split(" ");
                int wordsAlreadySpoken = (int)Math.round((pauseTimestamp - startTimestamp)*WORDS_PER_MS);
                words = Arrays.copyOfRange(words, wordsAlreadySpoken-1, words.length);
    
                textToSpeechBuilder = new StringBuilder();
                for(String s : words){
                    textToSpeechBuilder.append(s);
                    textToSpeechBuilder.append(" ");
                }
            } else {
                textToSpeechBuilder = new StringBuilder(getResources().getString(R.string.talkback_tips));
            }
    
            if (tts != null && languageAvailable)
                speak(textToSpeechBuilder.toString(), TextToSpeech.QUEUE_FLUSH, new Bundle(), "utter");
        }
    }
    

提交回复
热议问题