语言识别和聊天机器人的实现

最后更新于:2022-04-01 14:32:00

安卓上面语音识别和聊天机器人是非常常见也是非常实用的功能,在这里主要分享一款非常著名的语言平台,[科大迅飞开放平台](http://www.xfyun.cn/)。 首先从官网下载一个sdk的包,如下图: ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-14_56e65d6b687bb.jpg) 我们可以新建一个安卓工程,取名Robot,将下载下来的文件中的libs中的文件拷贝到我们的项目中的libs目录下,然后写一下布局文件: activity_main.xml ~~~ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/lv_list" android:layout_width="wrap_content" android:layout_height="0dp" android:divider="@null" android:layout_weight="1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/bottom_bar" android:gravity="center" android:orientation="vertical" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@drawable/btn_selector" android:onClick="startListen" android:text="点击开始语音识别" android:textColor="#000" android:textSize="16sp" /> </LinearLayout> </LinearLayout> ~~~ list_item.xml ~~~ <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/tv_ask" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_margin="5dp" android:background="@drawable/asker_bubble" android:gravity="center" android:text="你吃饭了吗?" android:textColor="#000" android:textSize="16sp" /> <LinearLayout android:id="@+id/ll_answer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_ask" android:layout_margin="5dp" android:background="@drawable/answer_bubble" android:orientation="vertical" > <TextView android:id="@+id/tv_answer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:text="" android:textColor="#000" android:textSize="16sp" /> <ImageView android:id="@+id/iv_pic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/m" android:visibility="gone" /> </LinearLayout> </RelativeLayout> ~~~ 界面效果如下: ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-14_56e65d6b98895.jpg) 我们可以在主界面中实现上面的功能: // 初始化语音引擎 SpeechUtility.createUtility(this, SpeechConstant.APPID + "=54b8bca3"); ~~~ /** * 语音朗诵 */ public void read(String text) { SpeechSynthesizer mTts = SpeechSynthesizer .createSynthesizer(this, null); mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan"); mTts.setParameter(SpeechConstant.SPEED, "50"); mTts.setParameter(SpeechConstant.VOLUME, "80"); mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); mTts.startSpeaking(text, null); } /** * 开始语音识别 * * @param view */ public void startListen(View view) { RecognizerDialog iatDialog = new RecognizerDialog(this, null); // 2.设置听写参数,详见《科大讯飞MSC API手册(Android)》SpeechConstant类 iatDialog.setParameter(SpeechConstant.DOMAIN, "iat"); iatDialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn"); iatDialog.setParameter(SpeechConstant.ACCENT, "mandarin"); iatDialog.setListener(recognizerDialogListener); iatDialog.show(); } class ChatAdapter extends BaseAdapter { @Override public int getCount() { return mChatList.size(); } @Override public ChatBean getItem(int position) { return mChatList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); convertView = View.inflate(MainActivity.this, R.layout.list_item, null); holder.tvAsk = (TextView) convertView.findViewById(R.id.tv_ask); holder.tvAnswer = (TextView) convertView .findViewById(R.id.tv_answer); holder.llAnswer = (LinearLayout) convertView .findViewById(R.id.ll_answer); holder.ivPic = (ImageView) convertView .findViewById(R.id.iv_pic); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } ChatBean item = getItem(position); if (item.isAsker) {// 是提问者 holder.tvAsk.setVisibility(View.VISIBLE); holder.llAnswer.setVisibility(View.GONE); holder.tvAsk.setText(item.text); } else { holder.tvAsk.setVisibility(View.GONE); holder.llAnswer.setVisibility(View.VISIBLE); holder.tvAnswer.setText(item.text); if (item.imageId != -1) {// 有图片 holder.ivPic.setVisibility(View.VISIBLE); holder.ivPic.setImageResource(item.imageId); } else { holder.ivPic.setVisibility(View.GONE); } } return convertView; } } static class ViewHolder { public TextView tvAsk; public TextView tvAnswer; public LinearLayout llAnswer; public ImageView ivPic; } /** * 解析语音数据 * * @param resultString */ protected String parseData(String resultString) { Gson gson = new Gson(); VoiceBean bean = gson.fromJson(resultString, VoiceBean.class); ArrayList<WSBean> ws = bean.ws; StringBuffer sb = new StringBuffer(); for (WSBean wsBean : ws) { String text = wsBean.cw.get(0).w; sb.append(text); } return sb.toString(); } ~~~ 当启动的时候是这样的![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-14_56e65d6be8f4e.jpg) 更多的功能我们可以去看开发者文档。
';