直接上代码,复制粘贴直接使用
TextToSpeech tts;
//语音
private void Speak(final String str) {
tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@SuppressWarnings("deprecation")
@Override
public void onInit(int status) {
//初始化成功的话,设置语音
if (status == TextToSpeech.SUCCESS) {
tts.setLanguage(Locale.CHINESE);
tts.speak(str, TextToSpeech.QUEUE_FLUSH, null);
}
}
});
}
使用时直接
Speak("播报的语音内容");
注:经测试部分手机型号不能播报