private TextToSpeech tts;
private void play() {
tts = new TextToSpeech(this,new listener());
}
private class listener implements TextToSpeech.OnInitListener {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
//设置播放语言
int result = tts.setLanguage(Locale.CHINESE);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){
Toast.makeText(VoiceActivity.this, "不支持", Toast.LENGTH_SHORT).show();
}else if(result == TextToSpeech.LANG_AVAILABLE){
//初始化成功之后才可以播放文字
//否则会提示“speak failed: not bound to tts engine
//TextToSpeech.QUEUE_ADD会将加入队列的待播报文字按顺序播放
//TextToSpeech.QUEUE_FLUSH会替换原有文字
tts.speak("需要转化的文字",TextToSpeech.QUEUE_ADD,null);
}
} else {
Log.e("TAG", "初始化失败");
}
}
public void stopTTS() {
if ( tts != null) {
tts .shutdown();
tts .stop();
tts = null;
}
}
01-06
11-26
4688

01-19
809
