gpt4 book ai didi

android - 如何将阿拉伯语设置为语言环境

转载 作者:行者123 更新时间:2023-11-29 14:36:31 25 4
gpt4 key购买 nike

我正在从事文本到语音的转换。为此,我从互联网上得到了例子。在此,他们通过 setLanguage(Locale.US); 设置英语。所以,现在我正在尝试设置阿拉伯语而不是英语。但是当我将语言更改为阿拉伯语时失败了。谁帮我把语言改成阿拉伯语

引用代码

import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class TexttoSpeechActivity extends Activity implements OnInitListener {
/** Called when the activity is first created. */

private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tts = new TextToSpeech(this, this);
btnSpeak = (Button) findViewById(R.id.btnSpeak);
txtText = (EditText) findViewById(R.id.txtText);

btnSpeak.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {
speakOut();
}

});
}

@Override
public void onDestroy() {
// Don't forget to shutdown!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}

public void onInit(int status) {
// TODO Auto-generated method stub

if (status == TextToSpeech.SUCCESS) {

/*Locale locale = new Locale("ar_EG");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());*/

int result = tts.setLanguage(Locale.US);

if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(this, "Language not supported", Toast.LENGTH_LONG).show();
Log.e("TTS", "Language is not supported");
} else {
btnSpeak.setEnabled(true);

}

} else {
Log.e("TTS", "Initilization Failed");
}

}

private void speakOut() {

String text = txtText.getText().toString();
if (text.length() == 0) {
tts.speak("You haven't typed text", TextToSpeech.QUEUE_FLUSH, null);
} else {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}

}
}

最佳答案

在最近的引用资料中,Locale 包含适用于 ISO 639-1/ISO 3166-1 的语言/国家/地区代码信息。

ISO 639-1 是两个字母的小写语言代码。阿拉伯语以这种格式定义 ar

所以,试试这个代码:

Locale loc = new Locale("ar");
/* under API 20 */
tts.setLanguage(loc);

/* over API 21 */
String voiceName = loc.toLanguageTag();
Voice voice = new Voice(voiceName, loc, Voice.QUALITY_HIGH, Voice.LATENCY_HIGH, false, null);
tts.setVoice(voice);

此外,如果您想收听阿拉伯语语音,您使用的TextToSpeech 服务必须支持阿拉伯语。先检查一下。

注意:

Android 语言环境引用:https://developer.android.com/reference/java/util/Locale.html

ISO 639-1 代码引用:https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes

关于android - 如何将阿拉伯语设置为语言环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35315139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com