Android 如何将阿拉伯语设置为区域设置

Android 如何将阿拉伯语设置为区域设置,android,locale,text-to-speech,Android,Locale,Text To Speech,我正在进行文字到语音的转换。为此,我从互联网上得到了一个例子。在这方面,他们通过setLanguage(Locale.US)设置英语。所以,现在我尝试设置阿拉伯语而不是英语。但当我把语言改成阿拉伯语时,我失败了。有人帮我把语言改成阿拉伯语吗 参考代码 import java.util.Locale; import android.app.Activity; import android.os.Bundle; import android.speech.tts.TextToSpeech; impo

我正在进行文字到语音的转换。为此,我从互联网上得到了一个例子。在这方面,他们通过
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);
        }

    }
}

参考资料:

在最近的参考资料中,
地区
包含适用于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区域设置参考:


ISO 639-1代码参考:

首先检查您的设备是否支持阿拉伯语,如果它支持您的设备,那么您应该将其用于TTS。您好,Hussnain Azam,之前我看到过这个示例,但我未能将其设置为TTS.setLanguage(Locale.US);你能考虑一下吗?Locale=newlocale(“ru”);tts.setLanguage(语言环境);您可以创建任何自定义区域设置,并将其设置为languagear=arabic,ru=Russian,但TextToSpeech类不支持“fa”或“ar”!有人有解决办法吗?你是说“波斯语(fa)”还是“阿拉伯语(ar)”,对吗?也许谷歌TTS不支持这种语言。您可能会找到另一个支持这些语言的TTS引擎,或者等待Google TTS更新以支持这些语言。如果您使用Google TTS,请查看支持的语言列表:是,我使用Google TTS。显然,为了阅读像土耳其语这样的语言,还需要在手机上安装谷歌TTS应用程序。是真的吗?是的,是真的。但谷歌TTS默认安装在Android手机上。如果没有,您可以通过App store进行安装。请参阅上面的链接。
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);