美文网首页Android
TTS语音合成技术

TTS语音合成技术

作者: 瑟闻风倾 | 来源:发表于2017-11-22 11:07 被阅读40次

HomeActivity

(1)public TextToSpeech textToSpeech;

(2)onCreate()中调用

private void initTextToSpeech(Context context){

// 传入context及onInitListener

textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {

@Override

public void onInit(int status) {

if (status == TextToSpeech.SUCCESS) {//装载TTS引擎成功

//Locale.getDefault()获取系统默认的区域信息:如系统语言设置为中文则参数为 "zho","CHN",设置为美式英语则参数为 "eng","USA"

int result = textToSpeech.setLanguage(new Locale(Locale.getDefault().getISO3Language(),Locale.getDefault().getISO3Country()));

Log.v("ttSpeech_result",result+","+ Locale.getDefault().getISO3Language() + ","+ Locale.getDefault().getISO3Country() + "");//如果打印为-2,说明不支持这种语言

if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){

Toast.makeText(activity, getString(R.string.missing_or_unsupported), Toast.LENGTH_SHORT).show();

}else {

textToSpeech.setPitch(1.0f);// 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规

textToSpeech.setSpeechRate(1.0f);// 设置语速

textToSpeech.speak(settings.getUserName()+getString(R.string.hello), TextToSpeech.QUEUE_FLUSH, null);//settings.getUserName()

}

/*int result1 = textToSpeech.setLanguage(Locale.US);//设置使用美式英语朗读

int result2 = textToSpeech.setLanguage(Locale.CHINESE);//设置支持中文,实现不了只能用讯飞或者百度的sdk

Log.v("ttSpeech_result",result1 + "," + result2 + "");//如果打印为-2,说明不支持这种语言

if (result1 == TextToSpeech.LANG_MISSING_DATA || result1 == TextToSpeech.LANG_NOT_SUPPORTED

|| result2 == TextToSpeech.LANG_MISSING_DATA || result2 == TextToSpeech.LANG_NOT_SUPPORTED) {

Toast.makeText(activity, "数据丢失或不支持", Toast.LENGTH_SHORT).show();

}else {

textToSpeech.setPitch(1.0f);// 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规

textToSpeech.setSpeechRate(1.0f);// 设置语速

textToSpeech.speak(settings.getUserName()+"你好", TextToSpeech.QUEUE_FLUSH, null);

}*/

}else {

//Log.v("ttSpeech_errorResult","never expect to see this result code");

Toast.makeText(activity, getString(R.string.load_tts_fail), Toast.LENGTH_SHORT).show();

}

}

});

}

(3)onDestroy()

if (textToSpeech != null) {

textToSpeech.stop();

textToSpeech.shutdown();

}

备注:

基础版见:https://www.jianshu.com/p/ba74a92afbbe

进阶版见:https://www.jianshu.com/p/e2995383113b

相关文章

网友评论

    本文标题:TTS语音合成技术

    本文链接:https://www.haomeiwen.com/subject/bajnvxtx.html