除英语之外,“文字转语音”无法在API级别30上运行

问题描述

我以“印地语”语言实现了文本到语音的转换,这是我的印度语言,我的应用程序在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,null);
            } else {
                HashMap<String,String> param = new HashMap<>();
                param.put(TextToSpeech.Engine.KEY_ParaM_STREAM,String.valueOf(AudioManager.STREAM_MUSIC));
                textToSpeech.speak(s,param);
                textToSpeech.speak(text,param);
            }
        }catch (Exception ae){
            ae.printstacktrace();
        }
    }

根据新文档。我还在清单标记添加一个查询标记

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

解决方法

在manifest文件中添加如下权限;

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

,

谢谢穆斯塔法巴林

android studio 让我改变了一点。

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...