关于android语音播报
private TextToSpeech textToSpeech = null;//创建自带语音对象
private void initTTS() {
//实例化自带语音对象
textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == textToSpeech.SUCCESS) {
// Toast.makeText(MainActivity.this,“成功输出语音”,
// Toast.LENGTH_SHORT).show();
// Locale loc1=new Locale(“us”);
// Locale loc2=new Locale(“china”);
textToSpeech.setPitch(0.5f);//方法用来控制音调
textToSpeech.setSpeechRate(0.01f);//用来控制语速
//判断是否支持下面两种语言
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 {
// MyToast.showToast(“数据丢失或不支持”);
// Toast.makeText(MainActivity.this, “数据丢失或不支持”, Toast.LENGTH_SHORT).show();
}
}
});
} private void initTTS() {
//实例化自带语音对象
textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == textToSpeech.SUCCESS) {
// Toast.makeText(MainActivity.this,"成功输出语音",
// Toast.LENGTH_SHORT).show();
// Locale loc1=new Locale("us");
// Locale loc2=new Locale("china");
textToSpeech.setPitch(0.5f);//方法用来控制音调
textToSpeech.setSpeechRate(0.01f);//用来控制语速
//判断是否支持下面两种语言
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 {
// MyToast.showToast(“数据丢失或不支持”);
// Toast.makeText(MainActivity.this, “数据丢失或不支持”, Toast.LENGTH_SHORT).show();
}
}
});
}
private void startAuto(String data, boolean b) {//播放的函数方法
// 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
textToSpeech.setPitch(1.f);
Log.d("wpnqq", "startAuto: " + b);
// 设置语速
textToSpeech.setSpeechRate(8.01f);
textToSpeech.speak(data,//输入中文,若不支持的设备则不会读出来
TextToSpeech.QUEUE_FLUSH, null);
}
注意:本机在华为手机运行,本地语言要改成中文