最近收到了结合推送 到的内容 语音播报功能实现
就想到sdk内置
android.speech 已经内置了语音播放功能
开始放代码
//创建自带语音对象
private TextToSpeech textToSpeech = null;
/**
* 初始化TextToSpeech对象
*/
private void initTTS() {
//实例化自带语音对象
textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == 0) {
// Toast.makeText(MainActivity.this,"成功输出语音",
// Toast.LENGTH_SHORT).show();
// Locale loc1=new Locale("us");
// Locale loc2=new Locale("china");
textToSpeech.setPitch(1.0f);//方法用来控制音调
textToSpeech.setSpeechRate(1.0f);//用来控制语速
//判断是否支持下面两种语言
int result1 = textToSpeech.setLanguage(Locale.US);
int result2 = textToSpeech.setLanguage(Locale.
SIMPLIFIED_CHINESE);
boolean a = (result1 == TextToSpeech.LANG_MISSING_DATA || result1 == TextToSpeech.LANG_NOT_SUPPORTED);
boolean b = (result2 == TextToSpeech.LANG_MISSING_DATA || result2 == TextToSpeech.LANG_NOT_SUPPORTED);
Log.i("zhh_tts", "US支持否?--》" + a +
"\nzh-CN支持否》--》" + b);
} else {
Toast.makeText(MainActivity.this, "数据丢失或不支持", Toast.LENGTH_SHORT).show();
}
}
});
}
开启播报:
textToSpeech.setLanguage(Locale.JAPANESE);
//设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
textToSpeech.setPitch(4f);
//设置语速
textToSpeech.setSpeechRate(3f);
//输入中文,若不支持的设备则不会读出来
textToSpeech.speak(“要播的内容文本”,
TextToSpeech.QUEUE_FLUSH, null);
使用Android/sdk/sources/android-28/speech/tts/TextToSpech实现语音播报功能就这些了 祝君好运
本文地址:https://blog.csdn.net/weixin_40611659/article/details/110528886