如何在我的应用程序中启用阿拉伯语?

时间:2015-03-12 19:14:52

标签: android

我尝试了将文本转换为语音的代码,但如果文本是用阿拉伯语编写的话,我希望语言是阿拉伯语。 我查看了与此主题相关的其他问题,但没有任何关系与我合作

布局包含编辑文本,按钮和TextView。



   


public class AndroidTextToSpeechActivity extends Activity implements
TextToSpeech.OnInitListener {
  private int result=0;
  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);
    //button on click event
    btnSpeak.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View arg0) {
          speakOut();
        }
    });
  }
  //shutdown tts when activity destroy
  @Override
  public void onDestroy() {
  // Don't forget to shutdown!
  if (tts != null) {
    tts.stop();
    tts.shutdown();
   }
   super.onDestroy();
  }
  //It will called before TTS started
  @Override
  public void onInit(int status) {
 
  /
  if (status == TextToSpeech.SUCCESS) {
 
  result = tts.setLanguage(Locale.US);



 if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
   Toast.makeText(this, "Missing data", Toast.LENGTH_LONG).show();
  
   btnSpeak.setEnabled(false);
  } else {
   
   btnSpeak.setEnabled(true);
   }
  } else {
      Log.e("TTS", "Initilization Failed");
     }
  }
  //call this method to speak text
  private void speakOut() {
  String text = txtText.getText().toString();
  if(result!=tts.setLanguage(Locale.US))
  {
  Toast.makeText(getApplicationContext(), "Enter right Words...... ", Toast.LENGTH_LONG).show();
  }else
   {
    //speak given text
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
   }
  }
}




1 个答案:

答案 0 :(得分:0)

好吧,你确实将tts语言设置为Locale.US,这就是它说英语的原因。如果你的tts引擎有阿拉伯语语音数据,你可以使用你已经使用的setLanguage方法将语言环境设置为“ar”。

目前尚不清楚你是否还需要自动确定文字写入的语言,这是一个完全不同的故事。