我用“印地语”实现了文本到语音转换。这是一种印度语言,我的应用程序在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标记。
<queries>
...
<intent>
<action android:name="android.intent.action.TTS_SERVICE" />
</intent>
</queries>
4条答案
按热度按时间xn1cxnb41#
在清单文件中添加以下权限;
用户权限android:名称=“android.权限.QUERY_ALL_PACKAGES”
h79rfbju2#
感谢你的评分
不过android studio让我做了一点改动。
2j4z5cfb3#
在Android 10、11和12上测试。
e3bfsja24#
我也遇到过同样的问题,下面是我的经验。
我有3个测试设备,其中一个是Pixel 3A(Android11),在没有声明“TextToSpeech.Engine.INTENT_ACTION_TTS_SERVICE”在清单文件的查询元素中的情况下,它的文本到语音转换功能可以很好地工作。
但文本到语音功能没有工作在我的红米K30Pro(Android12)和红米K50超(Android12),除非我声明“文本到语音引擎.INTENT_ACTION_TTS_SERVICE”在清单文件中。