java - 如何使文本在 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");
}
}
有什么建议吗?
解决方案
推荐阅读
- sympy - 使用 sympy 从字符串中解析一系列公式
- python-2.7 - 在 Google Sheets API 教程中使用 tools.run_flow() 会引发 SSLHandshake“证书验证”错误
- java - 如何获取已创建的 MatchAllFilters 和其他过滤器的名称?需要对界面进行哪些更改?
- unity3d - Assets -> Create Sprite 和 GameObject -> Create 2D Sprite Object 之间有什么区别吗
- python - 创建 tf.constant 给定的索引和值
- go - 支持用引号括起来的参数的 Slackbot
- python-3.x - 如何将带有txt和ids的.txt文件内容解析为python中的字典?
- ruby-on-rails - 本地和服务器之间的 UTC 转换不同
- python - Azure ML Studio:如何在通过数据处理之前使用 Python 更改输入值
- html - 使用 html slim 将类分配给 link_to