TextView content read aloud without setOnClickListener (Automatic Text-To-Speech)

假装没事ソ 提交于 2019-12-13 04:01:08

问题


I am using the source code of a Text-To-Speech app. I would like the content of a TextView to be read aloud automatically (i.e. without the use of setOnClickListener). Does anyone know how to approach this? Thanks in advance!

EDIT: This is the final result, and it works:

mVoiceOutputTv.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        String text = charSequence.toString();
        if (text.contains(text)) {
            if (timer != null) {
                timer.cancel();
            }
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    toSpeak = mVoiceOutputTv.getText().toString();
                    textToSpeech.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
                }
            }, 1000);
        }
    }

    @Override
    public void afterTextChanged (Editable editable) {
        }
});

回答1:


I have not tested this code.Let me know if it doesn't work:

Timer timer;

textView.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if(timer != null) {
            timer.cancel();
        }
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                //Do your reading out here
            }
        },1000);//1000 milliseconds change it to whatever interval you want.
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
});


来源:https://stackoverflow.com/questions/54736693/textview-content-read-aloud-without-setonclicklistener-automatic-text-to-speech

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!