最近自己写了一个抽签的小程序,想着抽签之后会语音播报出来,于是就想到了简单的TextToSpeech。
首先实例化TextToSpeech
TextToSpeech texttospeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == texttospeech.SUCCESS) {
int result = texttospeech.setLanguage(Locale.CHINA);
if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE
&& result != TextToSpeech.LANG_AVAILABLE){
Toast.makeText(MainActivity.this, "暂不支持朗读!",Toast.LENGTH_SHORT).show();
}
}
}
});
//将TextView中的内容得到并播放出来
texttospeech.speak(tv_result.getText().toString(),TextToSpeech.QUEUE_ADD, null);
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (texttospeech != null)
texttospeech.shutdown();
super.onDestroy();
}
在AndroidManifest.xml中加入权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
这样就结束了,此文章仅仅提供自己以后方便使用