Android 语音播报

private TextToSpeech tts;

在oncreate中初始化:

tts = new TextToSpeech(this, new OnInitListener() {


@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == tts.SUCCESS) {
// Toast.makeText(MainActivity.this,"成功输出语音",
// Toast.LENGTH_SHORT).show();
// Locale loc1=new Locale("us");
// Locale loc2=new Locale("china");
int result1 = tts.setLanguage(Locale.US);
int result2 = tts.setLanguage(Locale.CHINESE);
if (result1 == TextToSpeech.LANG_MISSING_DATA
|| result1 == TextToSpeech.LANG_NOT_SUPPORTED
|| result2 == TextToSpeech.LANG_MISSING_DATA
|| result2 == TextToSpeech.LANG_NOT_SUPPORTED)


{
Toast.makeText(Xueya.this, "数据丢失或不支持",
Toast.LENGTH_SHORT).show();
}
}
}
});


//在需要语音播报的地方

//tts.speak("语音播报的内容", TextToSpeech.QUEUE_FLUSH, null);

猜你喜欢

转载自blog.csdn.net/huangliniqng/article/details/77972611