【发布时间】:2022-01-18 17:24:18
【问题描述】:
我正在开发一个应用程序,它使用 MLKit 的 OCR 功能从图像中读取文本,将其显示给用户,然后使用 Android 的 TTS,但它似乎无法正常工作。
private TextToSpeech textReader; // Instance of Android's built in TTS
private String multipleBlockText; // Empty to store multiple blocks
// Initialise TTS
textReader = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
textReader.setLanguage(Locale.UK); // Sets language to US, English.
}
});
// OCR code goes here
@Override
public void onClick(View v) {
try {
detectText(); // Calls function for OCR from LKit library
// Reads text
textReader.speak(text, TextToSpeech.QUEUE_FLUSH, null);
} catch(Exception error){ // Error handling: Prevents app crash on no text readable
detectedText.setText("Error!");
}
}
我也在我的清单中声明了 TTS:
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.TTS_SERVICE" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
但是当我运行我的应用程序时,它会向用户显示图像文本,但 TTS 不会大声朗读它。 OCR 功能工作正常,图像中的文本存储在“multipleBlockText”变量中。
【问题讨论】:
标签: java android android-studio text-to-speech