【发布时间】:2023-01-29 22:14:34
【问题描述】:
在 Android 应用程序中,我有一个文本,在用户按下按钮后,它将通过 TTS 朗读:
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
final Button speech = findViewById(R.id.speech);
speech.setOnClickListener(speech1 -> {
if (!isPackageInstalled("com.google.android.tts")) {
showMsgSnack(getString(R.string.noTTS));
} else {
Boolean speak = checkSpeak();
if (!speak) {
speech.setCompoundDrawablesWithIntrinsicBounds(R.drawable.stop, 0, 0, 0);
if (history.length() > 3999) {
String var = history.substring(0, 3999);
ConvertTextToSpeech(var, "test");
String var2 = history.substring(3999);
ConvertTextToSpeech(var2, "test");
} else {
ConvertTextToSpeech(history, "test");
}
saveSpeak(true);
} else {
speech.setCompoundDrawablesWithIntrinsicBounds(R.drawable.play, 0, 0, 0);
tts.stop();
saveSpeak(false);
}
}
});
private void ConvertTextToSpeech(String history, String par) {
String readableText = fromHtml(history).toString(); //remove HTML tags -> do not read <br>
Bundle params = new Bundle();
params.putString(KEY_PARAM_UTTERANCE_ID, "");
tts.speak(readableText, TextToSpeech.QUEUE_ADD, params, par);
}
关于 TTS 的东西:
if ((isPackageInstalled("com.google.android.tts"))) {
tts=new TextToSpeech(SingleitemView.this, status -> {
if(status == TextToSpeech.SUCCESS){
if (isLangAvailable(this,tts,locale)) {tts.setLanguage(locale);}
else {tts.setLanguage(new Locale("en"));}
tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onDone(String utteranceId) {
// Log.d("Speak", "TTS finished");
if (utteranceId.equals("test")) {
saveSpeak(false);
runOnUiThread(() -> {
Button view2 = findViewById(R.id.speech);
view2.setCompoundDrawablesWithIntrinsicBounds(R.drawable.play, 0, 0, 0);
});
}
}
@Override
public void onError(String utteranceId) {
}
@Override
public void onStart(String utteranceId) {
}
});
}
},"com.google.android.tts");}
这多年来一直没有问题,TTS 阅读文本,即使它很短,或超过 3999 个字符。几个月前突然(我这边的应用程序没有任何变化)当它开始说出更长的文本时,前 3-4 个单词在每个文本中都被扭曲并且几乎无法理解。
看起来它在阅读文本的同时在后台做某事。
不确定发生了什么,因为我没有更改我的代码并且它一直有效到现在。
所以现在我尝试更新所有库和依赖项,但没有帮助。
问题仅在于文本,它超过 3999 个字符。较短的文本没有问题。
我也尝试将这个条件放在 onClickListener 之外,所以先准备文本:
if (history.length() > 3999) {
String var = fromHtml(history.substring(0, 3999)).toString();
String var2 = fromHtml(history.substring(3999)).toString(); }
然后在 onClickListener 中我只调用 TTS 两次:
ConvertTextToSpeech(var, "test");
ConvertTextToSpeech(var2, "test");
private void ConvertTextToSpeech(String history, String par) {
Bundle params = new Bundle();
params.putString(KEY_PARAM_UTTERANCE_ID, "");
tts.speak(history, TextToSpeech.QUEUE_ADD, params, par);
}
但我有同样的问题,这个解决方案没有帮助。
我认为问题是我立即调用 Convert Text To Speech 一个接一个,但即使我删除了第二个,问题仍然存在。
所以肯定地,问题出在较长的文本上,但我找不到它开始发生的原因的解决方案。我在很多设备上检查过这个,到处都是同样的问题。
唯一有帮助的是创建更小的文本块:
for (int a=1; a <= history.length(); a+=100) {
if((history.length() - (a + 100)) > 0) {ConvertTextToSpeech(history.substring(a, a+100), "test");}
else {ConvertTextToSpeech(history.substring(a), "test");}
然而,这会导致在说出组块之间出现明显的停顿,通常也在单词内部,因此这不是一个好的解决方案。
【问题讨论】: