1. La clase TextToSpeech viene con Android, pero algunos dispositivos necesitan admitir TTS y agregar una biblioteca de voz. Yo uso iFlytek Voice (sin conexión). Descargue e instale el APK de iFlytek Voice usted mismo y luego vaya a la configuración del sistema para configurar la función TTS para usar esta opción de forma predeterminada. Si tiene su propia biblioteca TTS, puede omitir este paso.
2. Definir el objeto TTS
private TextToSpeech mTextToSpeech=null;
3. Llamada, cada llamada debe inicializarse
try{
final String strSpeekNow = strSpeek;
mTextToSpeech=new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status==TextToSpeech.SUCCESS) {
//设置朗读语言
int supported=mTextToSpeech.setLanguage(Locale.CHINESE);
if ((supported!=TextToSpeech.LANG_AVAILABLE)&&(supported!=TextToSpeech.LANG_COUNTRY_AVAILABLE)) {
CardManager.toast(VerificationResultActivity.this, "不支持当前语言!");
}
}
//设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
mTextToSpeech.setPitch(1.0f);
//设置语速
mTextToSpeech.setSpeechRate(1.3f);
mTextToSpeech.speak(strSpeekNow, TextToSpeech.QUEUE_FLUSH, null, null);
}
});
} catch (Exception e) {//语音播报出错
Log.e("tts",e.toString());
}
4. Cerrar TTS en onDestroy
if (mTextToSpeech!=null) {
mTextToSpeech.shutdown();//关闭TTS
}