#Day06 #
6.1 聊天机器人 ##
项目演示
美女
你好
天王盖地虎
你是谁
布局搭建
ListView + LinearLayout
语音识别
参照科大讯飞sdk相关文档
/**
-
开始语音识别
* -
@param view
*/
public void startVoice(View view) {
RecognizerDialog iatDialog = new RecognizerDialog(this, mInitListener);iatDialog.setParameter(SpeechConstant.DOMAIN, "iat"); iatDialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn"); iatDialog.setParameter(SpeechConstant.ACCENT, "mandarin "); iatDialog.setListener(recognizerDialogListener); iatDialog.show(); }
数据解析
/**
-
解析语音json数据
*/
private String parseJson(String json) {
Gson gson = new Gson();
VoiceBean bean = gson.fromJson(json, VoiceBean.class);
ArrayList ws = bean.ws;StringBuffer sb = new StringBuffer(); for (WsBean wsBean : ws) { ArrayList<CwBean> cw = wsBean.cw; for (CwBean cwBean : cw) { String w = cwBean.w; sb.append(w); } } System.out.println("解析数据为:" + sb.toString()); return sb.toString(); }
ListView数据展示
item布局处理(提问+回答)
语音朗诵
/**
- 机器人讲话
*/
private void speak(String answer) {
// 1.创建SpeechSynthesizer对象, 第二个参数:本地合成时传InitListener
SpeechSynthesizer mTts = SpeechSynthesizer
.createSynthesizer(this, null);
// 2.合成参数设置,详见《科大讯飞MSC API手册(Android)》SpeechSynthesizer 类
mTts.setParameter(SpeechConstant.VOICE_NAME, “vixying”);// 设置发音人
mTts.setParameter(SpeechConstant.SPEED, “50”);// 设置语速
mTts.setParameter(SpeechConstant.VOLUME, “80”);// 设置音量,范围0~100
// 设置合成音频保存位置(可自定义保存位置),保存在“./sdcard/iflytek.pcm”
// 保存在SD卡需要在AndroidManifest.xml添加写SD卡权限
// 如果不需要保存合成音频,注释该行代码
mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH, “./sdcard/iflytek.pcm”);
// 3.开始合成
mTts.startSpeaking(answer, null);
}