android - API 级别 30 为印地语提供了不受支持的语言错误代码
问题描述
我实现了文本到语音,它在印地语的 android API 级别 28 之前工作正常,但在 Android API 级别 30 中它返回错误代码 -2(不支持语言)。
private void setTextTospeech() {
textToSpeech = new TextToSpeech(mContext, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
if (language.toLowerCase().contains(langaugeCodeEnglish)) {
int result = textToSpeech.setLanguage(new Locale("en", "IN"));
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
//Toast.makeText(mContext, result + " is not supported", Toast.LENGTH_SHORT).show();
Log.e("Text2SpeechWidget", result + " is not supported");
}
} else {
int result = textToSpeech.setLanguage(new Locale("hi", "IN"));
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
textToSpeech.setLanguage(Locale.forLanguageTag("hin"));
} else {
// Toast.makeText(mContext, result + "Language is not supported", Toast.LENGTH_SHORT).show();
Log.e("Text2SpeechWidget", result + "Language is not supported");
}
Log.e("Text2SpeechWidget", result + " is not supported");
}
}
}
}
});
}
private void speak(String s, String text) {
try{
float pitch = (float) 0.62;
float speed = (float) 0.86;
textToSpeech.setSpeechRate(speed);
textToSpeech.setPitch(pitch);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Bundle bundle = new Bundle();
bundle.putInt(TextToSpeech.Engine.KEY_PARAM_STREAM, AudioManager.STREAM_MUSIC);
textToSpeech.speak(s, TextToSpeech.QUEUE_FLUSH, bundle, null);
textToSpeech.speak(text, TextToSpeech.QUEUE_ADD, bundle, null);
} else {
HashMap<String, String> param = new HashMap<>();
param.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_MUSIC));
textToSpeech.speak(s, TextToSpeech.QUEUE_FLUSH, param);
textToSpeech.speak(text, TextToSpeech.QUEUE_ADD, param);
}
}catch (Exception ae){
ae.printStackTrace();
}
}
根据新文档。我还在清单标签中添加了一个查询标签。
<queries>
...
<intent>
<action android:name="android.intent.action.TTS_SERVICE" />
</intent>
</queries>
解决方案
推荐阅读
- python - 从列中获取低、高和均值
- ruby-on-rails - 如何指定 Ransack 在标题和正文中搜索 - Rails
- c# - 如何防止 VS 调试器在每次等待时显示异常?
- ios - 无法在 swift 4 中解析 JSON
- python - 如何从`scipy.odr`获得正交投影?
- django - 如果在设置中添加,LimitOffsetPagination 默认情况下在泛型视图中无法全局工作
- c - 如何将 SOCKET (HANDLE) 从非托管 C 程序传递给 .NET 子进程?
- android-studio - Andriod Studio Gradle 同步失败:无法从选定的 JDK.2 运行 JVM
- c - GNU,autotools 的 makefile - 构建 C 语言源代码的规则和配方
- php - MPDF:重置总页码