4

I'm working on text to speech conversion. For this, I got the example from the internet. In this they set the English language by setLanguage(Locale.US);. So, now I'm trying to set the Arabic instead of English. But I failed when I change the language to Arabic. Anyone help me to change the language as Arabic

Code for reference

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);
        }

    }
}
Alireza Noorali
  • 3,129
  • 2
  • 33
  • 80
Bahu
  • 1,516
  • 2
  • 28
  • 49

2 Answers2

7

In recent reference, Locale has information of language/country code which are suitable in ISO 639-1/ISO 3166-1.

ISO 639-1 is two-letter lowercase language code. Arabic is defined ar in this format.

So, try this code:

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);

Also, TextToSpeech service that you used must support Arabic if you want to listen Arabic voice. Check it first.

Note:

Android Locale reference: https://developer.android.com/reference/java/util/Locale.html

ISO 639-1 code reference: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Kae10
  • 619
  • 2
  • 6
  • 17
  • don't support "fa" or "ar" for TextToSpeech class! Does anyone have a solution? – roghayeh hosseini Sep 30 '18 at 14:32
  • 1
    Do you mean "Persian(fa)" or "Arabic(ar)", right? Maybe Google TTS doesn't support that languages. You may find another TTS engine that support them, or wait for Google TTS update to support that languages. – Kae10 Oct 04 '18 at 08:54
  • If you use Google TTS, check the list of supported languages: https://play.google.com/store/apps/details?id=com.google.android.tts&hl=en – Kae10 Oct 04 '18 at 08:56
  • Yes I use Google TTS. Apparently, in order to read languages like Turkish, it's also necessary to install the Google TTS app on the phone. It is true? – roghayeh hosseini Oct 05 '18 at 14:00
  • 1
    Yes, it's true. But Google TTS has installed on Android phone as default. If not, you can install it through App store. Refer above link. – Kae10 Oct 08 '18 at 01:23
2
Locale locale = new Locale("ru");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());

Reference :ref

Community
  • 1
  • 1
Hussnain Azam
  • 358
  • 1
  • 5
  • 14