如何将阿拉伯语言设置为Locale

Bah*_*ahu 1 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;
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);
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

Kae*_*e10 5

在最近的参考文献中,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);
Run Code Online (Sandbox Code Playgroud)

此外,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