除英语外,文本到语音不适用于API 30级



我用语言实现了文本到语音的转换"印地语";这是一个印度语言我的应用程序,工作良好,直到API 29级。它适用于英语,但不适用于印地语。但在API 30级的新设备中,它不起作用。在调试它的给定结果值-2"时;语言不支持错误";API 30级设备。

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();
}
}

根据新文档。我还在manifest标记中添加了一个查询标记。

<queries>
...
<intent>
<action android:name="android.intent.action.TTS_SERVICE" />
</intent>
</queries>

在清单文件中添加以下权限;

使用权限android:name="android.permission.QUERY_ALL_PACKAGES";

感谢Mustafa Balin

安卓工作室让我稍微改变了一下。

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />

也在没有这些权限的情况下工作。在Android 10、11和12上测试。

我也遇到了同样的问题,这是我的经验。

我有3个测试设备。其中之一是Pixel 3A(Android11(,其中文本到语音功能工作良好,而不声明";TextToSpeech.Engine.INTENT_ACTION_TTS_SERVICE";在清单文件的查询元素中。

但是文本到语音功能在我的RedMi K30Pro(Android12(和RedMi K50 Ultra(Android12中(上不起作用;TextToSpeech.Engine.INTENT_ACTION_TTS_SERVICE";在清单文件中。

最新更新