如何在TTs google api中使用其他语言而不是英语?

时间:2014-05-07 13:38:58

标签: android text-to-speech

我正在尝试使用TTS api将文本转换为语音,我尝试使用英语,但工作得很好,但我想将其转换为阿拉伯语或印地语。请问我该怎么做?

 public class TextToVoice extends Activity  implements TextToSpeech.OnInitListener{
        private TextToSpeech tts; //define object from TextToSpeech

        private Button btnSpeak;// define button
        private EditText txtText; // define edit text

        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.text_to_voice_screen);
            setTitle("Text Converter");

            tts = new TextToSpeech(this, this); // call object TextToSpeech


            btnSpeak = (Button) findViewById(R.id.btnSpeak); // for call button id

            txtText = (EditText) findViewById(R.id.txtText); // for call EditText id

            // button on click event
            btnSpeak.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    speakOut(); // call this function
                }

            });
        }


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

        @Override
        public void onInit(int status) { // for check language and check data in EditText


            if (status == TextToSpeech.SUCCESS) {
                int result = tts.setLanguage(Locale.US);

                if (result == TextToSpeech.LANG_MISSING_DATA
                        || result == TextToSpeech.LANG_NOT_SUPPORTED) {

                } else {
                    btnSpeak.setEnabled(true);
                    speakOut();
                }

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

        }

        private void speakOut() {

            String text = txtText.getText().toString(); // for get text from EditText

            tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); // for call speak methods

        }
    }

1 个答案:

答案 0 :(得分:0)

您在onInit中使用setLanguage:

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

以下是所有区域设置的列表:http://developer.android.com/reference/java/util/Locale.html

但我不确定android是否支持阿拉伯语/印地语的TTS。