android - Text to Speech 不能在 API 级别 30 中工作,而不是英语

标签 android android-studio android-manifest text-to-speech android-api-levels

我用“印地语”语言实现了文本到语音,它是一种印度语言,我的应用程序在 API 级别 29 之前运行良好。
它适用于英语,但不适用于印地语。
但在 API 级别 30 的新设备中,它不起作用。
在 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();
        }
    }
根据新文档。我还在 list 标签中添加了一个查询标签。
<queries>
   ...
  <intent>
      <action android:name="android.intent.action.TTS_SERVICE" />
  </intent>
 </queries>

最佳答案

在 list 文件中添加以下权限;
使用权限 android:name="android.permission.QUERY_ALL_PACKAGES"

关于android - Text to Speech 不能在 API 级别 30 中工作,而不是英语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63967419/

相关文章:

android - 在 Jetpack Compose 中单击时如何禁用涟漪效应

java - 请我需要有关 list 合并失败并出现多个错误的解决方案

android - 为 Android 应用程序设置系统属性

android - Android通知-setSound不起作用

Android SocketIOClient错误: "Scheme ' ws' not registered

android - 如何从存储在 android 的 sharedPreference 中的数组列表中删除项目?

android - 如何将自定义文件导入 APK

android - Umano SlidingUpPanel 上滑后顶部面板消失

java - 如何检查 android 库中的可调试或调试构建类型?

android - 应用程序的顶部栏不隐藏android