android也可以实现把输入的文字朗读出来,使用到的是TextToSpeech,不过目前只支持5种语言:English、 French 、 German 、 Italian 和 Spanish。(真遗憾,没有Chinese),对android系统要求为android
1.6(API8)以上:
public class SpeechTestActivity extends Activity {
/**TextToSpeech对象*/
private TextToSpeech mText2Speech;
/**确定按钮*/
private Button mBtn;
/**文本输入框*/
private EditText mEdt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewsById();
initListeners();
mBtn = (Button) findViewById(R.id.test_btn);
mEdt = (EditText) findViewById(R.id.test_edt);
mBtn.setEnabled(false);
}
private void initListeners() {
mText2Speech = new TextToSpeech(this, new OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {/**如果装载TTS成功*/
int result = mText2Speech.setLanguage(Locale.ENGLISH);/**有Locale.CHINESE,但是不支持中文*/
if (result == TextToSpeech.LANG_MISSING_DATA/**表示语言的数据丢失。*/
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {/**语言不支持*/
Toast.makeText(SpeechTestActivity.this, "我说不出口", Toast.LENGTH_SHORT).show();
} else {
mBtn.setEnabled(true);
mText2Speech.speak("I miss you", TextToSpeech.QUEUE_FLUSH,
null);
}
}
}
});
mBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mText2Speech.speak(mEdt.getText().toString(),
TextToSpeech.QUEUE_FLUSH, null);
}
});
}
private void findViewsById() {
mBtn = (Button) findViewById(R.id.test_btn);
mEdt = (EditText) findViewById(R.id.test_edt);
}
@Override
protected void onDestroy() {
if (mText2Speech != null) {
mText2Speech.stop();
mText2Speech.shutdown();
}
super.onDestroy();
}
}