【发布时间】:2020-04-16 17:31:38
【问题描述】:
我正在开发一个应用程序,其中我的 textview 由字符串和两个按钮组成。当我单击说话按钮时,文本将转换为语音。但我想在语音运行时突出显示该词。
这是我的文字转语音初始化:
textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
result = textToSpeech.setLanguage(Locale.ENGLISH);
textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {
Log.d(utteranceId, "TTS start");}
@Override
public void onDone(String utteranceId) {
Log.d(utteranceId, "TTS done");}
@Override
public void onError(String utteranceId) {
});
} else {
Toast.makeText(getApplicationContext(), "Feature is not Available", Toast.LENGTH_SHORT).show();
}
}
});
及其他代码:
private void speak() {
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(getApplicationContext(), "Feature is not Available", Toast.LENGTH_SHORT).show();
} else {
textToSpeech.setPitch(1f);
textToSpeech.setSpeechRate(0.8f);
HashMap<String, String> params = new HashMap<>();
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "utteranceId");
textToSpeech.speak(getString(R.string.storytxt), TextToSpeech.QUEUE_FLUSH, params);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (textToSpeech != null) {
textToSpeech.shutdown();
}
}
直到这里我没有遇到任何问题。现在我想突出显示文本。我不知道该怎么做。我到处搜索仍然没有这方面的线索。
我将字符串存储在 String.xml 中。
【问题讨论】:
标签: java android text-to-speech highlight