不用讯飞语音的原因就是离线语音有点贵。
如果感觉下面方法不够优雅,并且就播放几个固定的语音那就可以使用系统播放音频文件的方法。
如果系统中只有一个语音引擎pico TTs 那这样表示不支持中文的,可以下载一个讯飞语记安装一下更改系统TTS输出引擎就可以使用了
/**
* 类描述:文本语音播报<br/>
* 创建人:吴冬冬<br/>
* 创建时间:2018/5/10 09:39 <br/>
*/
public class SpeechUtils {
private static SpeechUtils singleton;
private TextToSpeech textToSpeech; // TTS对象
public static SpeechUtils getInstance() {
if (singleton == null) {
synchronized (SpeechUtils.class) {
if (singleton == null) {
singleton = new SpeechUtils();
}
}
}
return singleton;
}
public SpeechUtils() {
textToSpeech = new TextToSpeech(MyApplication.getContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int i) {
if (i == TextToSpeech.SUCCESS) {
int result = textToSpeech.setLanguage(Locale.CHINESE);
if (result != TextToSpeech.LANG_AVAILABLE && result != TextToSpeech.LANG_COUNTRY_AVAILABLE) {
CommonUtil.debug("123===", "不支持该语言");
}
textToSpeech.setPitch(1.0f);// 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
textToSpeech.setSpeechRate(1.0f);
}
}
});
}
public void speakText(String text) {
if (textToSpeech != null) {
/*
TextToSpeech.QUEUE_FLUSH:该模式下在有新任务时候会清除当前语音任务,执行新的语音任务
TextToSpeech.QUEUE_ADD:该模式下会把新的语音任务放到语音任务之后,等前面的语音任务执行完了才会执行新的语音任务
*/
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
public void shutDown(){
if (textToSpeech != null) {
textToSpeech.stop();
textToSpeech.shutdown();
textToSpeech = null;
}
}
}
网友评论