[英]Why does TextToSpeech fail to initialize in this class?
I have a class which is supposed to speak the description of a given entry from glossary.我有一个 class 应该说出词汇表中给定条目的描述。 The code is like this:
代码是这样的:
package college.projects.glossary;
import android.content.Intent;
import android.os.Build;
import android.speech.tts.TextToSpeech;
import android.widget.Toast;
import java.util.Locale;
public class GlossaryEntryPlayer extends Object implements IGlossaryEntryPlayer, TextToSpeech.OnInitListener {
protected MyActivity activity_;
private TextToSpeech textToSpeech_ = null;
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = textToSpeech_.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
MyUtils.UIDebug(this.activity_.getApplicationContext(), "Language support is bad");
} else {
MyUtils.UIDebug(this.activity_.getApplicationContext(), "Language support is OK");
}
} else {
MyUtils.UIDebug(this.activity_.getApplicationContext(), "TextToSpeech is bad");
}
}
public GlossaryEntryPlayer(MyActivity activity) {
super();
this.activity_ = activity;
textToSpeech_ = new TextToSpeech(this.activity_, this);
}
@Override
public boolean play(GlossaryEntry entry) {
if (null != entry) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
textToSpeech_.speak(entry.getDescription(), TextToSpeech.QUEUE_FLUSH, null, null);
return true;
} else {
MyUtils.UIDebug(this.activity_.getApplicationContext(), "Version is bad");
return false;
}
} else {
return false;
}
}
}
The problem is in onInit()
since I always get the message "TextToSpeech is bad" .问题出在
onInit()
因为我总是收到消息"TextToSpeech is bad" 。 What's wrong with this class?这个 class 有什么问题? Android emulator used is 5.1 WVGA API 30, RAM 512 MB, the default language is US-ENG.
使用的 Android 仿真器为 5.1 WVGA API 30,RAM 512 MB,默认语言为 US-ENG。 The laptop on which Android Studio runs is Lenovo AMD A4-G50, 4GB Ram, 1.8Ghz
运行 Android Studio 的笔记本电脑是 Lenovo AMD A4-G50, 4GB Ram, 1.8Ghz
According to Google , " Apps targeting Android 11 that use text-to-speech should declare TextToSpeech.Engine#INTENT_ACTION_TTS_SERVICE
in the queries elements of their manifest: "根据Google的说法,“针对 Android 11 且使用文本转语音的应用程序应在其清单的查询元素中声明
TextToSpeech.Engine#INTENT_ACTION_TTS_SERVICE
: ”
So, add this to your AndroidManifest.xml:因此,将其添加到您的 AndroidManifest.xml:
<queries>
<intent>
<action android:name="android.intent.action.TTS_SERVICE" />
</intent>
</queries>
Adding this before <application
tag worked for me.在
<application
标签之前添加它对我有用。 (Android Studio says Element queries is not allowed here though). (Android Studio 说这里不允许元素查询)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.