首页 > 解决方案 > 如何使文本在 TTS 朗读时自动滚动

问题描述

这是一个非常简单的应用程序。

我希望在说话时自动滚动视图。截至目前,我在朗读时突出显示文本。但是,当文本很长时,我希望文本容器在说出文本时自动向下滚动文本。

想象一下电影中的字幕。但是,我有文字而不是字幕。而不是演员说话,我有 TTS。任何帮助表示赞赏。

这是我的代码:

XML 设计:

<TextView
    android:id="@+id/text_view_id"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:gravity="top"
    android:textSize="60dp"
    android:textStyle="bold"
    android:text="It's possible to perform frequency separation just like in Photoshop, and there are three types of healing tools. Photopea supports actions as well, so it's possible to create frequency separation layers with a single click."
    android:hint=""

    />
<Button
    android:id="@+id/botao"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:text="Rodar" />

这是我的java代码:package com.example.myapplication;

导入android.graphics.Color;导入android.os.Bundle;导入 android.speech.tts.TextToSpeech; 导入 android.speech.tts.UtteranceProgressListener;导入 android.text.Spannable;导入 android.text.SpannableString;导入 android.text.Spanned;导入 android.text.method.ScrollingMovementMethod;导入 android.text.style.BackgroundColorSpan;导入 android.text.style.ForegroundColorSpan;导入android.util.Log;导入android.view.View;导入android.widget.Button;导入 android.widget.EditText;导入 android.widget.TextView;

导入androidx.appcompat.app.AppCompatActivity;

导入 com.example.myapplication.R;

公共类 MainActivity 扩展 AppCompatActivity 实现 TextToSpeech.OnInitListener,View.OnClickListener {

TextToSpeech tts;

String sentence = "It's possible to perform frequency separation just like in Photoshop, and there are three types of healing tools. Photopea supports actions as well, so it's possible to create frequency separation layers with a single click.";

TextView textView;
Button botao;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = findViewById(R.id.text_view_id);
    textView.setText(sentence);

    tts = new TextToSpeech(this, this);
    botao = (Button) findViewById(R.id.botao) ;
    botao.setOnClickListener(MainActivity.this);


}

// TextToSpeech.OnInitListener (for our purposes, the "main method" of this activity)
public void onInit(int status) {

    tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {

        @Override
        public void onStart(String utteranceId) {
            Log.i("XXX", "utterance started");
        }

        @Override
        public void onDone(String utteranceId) {
            Log.i("XXX", "utterance done");
        }

        @Override
        public void onError(String utteranceId) {
            Log.i("XXX", "utterance error");
        }

        @Override
        public void onRangeStart(String utteranceId,
                                 final int start,
                                 final int end,
                                 int frame) {
            Log.i("XXX", "onRangeStart() ... utteranceId: " + utteranceId + ", start: " + start
                    + ", end: " + end + ", frame: " + frame);

            // onRangeStart (and all UtteranceProgressListener callbacks) do not run on main thread
            // ... so we explicitly manipulate views on the main thread:
            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    Spannable textWithHighlights = new SpannableString(sentence);
                    textWithHighlights.setSpan(new BackgroundColorSpan(Color.YELLOW), start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                    textView.setText(textWithHighlights);
                    textView.setMovementMethod(new ScrollingMovementMethod());

                }
            });

        }

    });

}

public void startClicked(View ignored) {

    tts.speak(sentence, TextToSpeech.QUEUE_FLUSH, null, "doesn't matter yet");

}

@Override
public void onClick(View view) {
    tts.speak(sentence, TextToSpeech.QUEUE_FLUSH, null, "doesn't matter yet");
}

}

有什么建议吗?

标签: javaandroidtext-to-speech

解决方案


推荐阅读