上屏幕界面activity_main.xml:

  1. 语音识别界面
  2.  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical" >
  8.  
  9. <ListView
  10. android:id="@+id/lv_list"
  11. android:layout_width="wrap_content"
  12. android:layout_height="0dp"
  13. android:divider="@null"
  14. android:layout_weight="1" />
  15.  
  16. <LinearLayout
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:background="@drawable/bottom_bar"
  20. android:gravity="center"
  21. android:orientation="vertical" >
  22.  
  23. <Button
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:layout_margin="10dp"
  27. android:background="@drawable/btn_selector"
  28. android:onClick="startListen"
  29. android:text="点击开始语音识别"
  30. android:textColor="#000"
  31. android:textSize="16sp" />
  32. </LinearLayout>
  33.  
  34. </LinearLayout>

MainActivity:

  1. package com.itheima.voicerobot;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5.  
  6. import android.app.Activity;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.BaseAdapter;
  11. import android.widget.ImageView;
  12. import android.widget.LinearLayout;
  13. import android.widget.ListView;
  14. import android.widget.TextView;
  15.  
  16. import com.google.gson.Gson;
  17. import com.iflytek.cloud.RecognizerResult;
  18. import com.iflytek.cloud.SpeechConstant;
  19. import com.iflytek.cloud.SpeechError;
  20. import com.iflytek.cloud.SpeechSynthesizer;
  21. import com.iflytek.cloud.SpeechUtility;
  22. import com.iflytek.cloud.ui.RecognizerDialog;
  23. import com.iflytek.cloud.ui.RecognizerDialogListener;
  24. import com.itheima.voicerobot.VoiceBean.CWBean;
  25. import com.itheima.voicerobot.VoiceBean.WSBean;
  26.  
  27. public class MainActivity extends Activity {
  28. private ListView lvList;
  29. private ArrayList<ChatBean> mChatList = new ArrayList<ChatBean>();
  30. /*public class ChatBean {
  31. public String text;// 内容
  32. public boolean isAsker;// true表示提问者,否则是回答者
  33. public int imageId = -1;// 图片id
  34. public ChatBean(String text, boolean isAsker, int imageId) {
  35. this.text = text;
  36. this.isAsker = isAsker;
  37. this.imageId = imageId;
  38. }
  39. }*/
  40. private ChatAdapter mAdapter;
  41. private String[] mMMAnswers = new String[] { "约吗?", "讨厌!", "不要再要了!",
  42. "这是最后一张了!", "漂亮吧?" };
  43. private int[] mMMImageIDs = new int[] { R.drawable.p1, R.drawable.p2,
  44. R.drawable.p3, R.drawable.p4 };
  45. @Override
  46. protected void onCreate(Bundle savedInstanceState) {
  47. super.onCreate(savedInstanceState);
  48. setContentView(R.layout.activity_main);
  49. lvList = (ListView) findViewById(R.id.lv_list);
  50. mAdapter = new ChatAdapter();
  51. lvList.setAdapter(mAdapter);
  52. // 初始化语音引擎
  53. SpeechUtility.createUtility(this, SpeechConstant.APPID + "=54b8bca3");
  54. }
  55.  
  56. StringBuffer mTextBuffer = new StringBuffer();
  57. //语音识别
  58. private RecognizerDialogListener recognizerDialogListener = new RecognizerDialogListener() {
  59. @Override
  60. public void onResult(RecognizerResult results, boolean isLast) {
  61. // System.out.println(results.getResultString());
  62. // System.out.println("isLast=" + isLast);
  63. String text = parseData(results.getResultString());
  64. mTextBuffer.append(text);
  65. if (isLast) {// 会话结束
  66. String finalText = mTextBuffer.toString();
  67. mTextBuffer = new StringBuffer();// 清理buffer
  68. System.out.println("最终结果:" + finalText);
  69. mChatList.add(new ChatBean(finalText, true, -1));
  70. String answer = "没听清";
  71. int imageId = -1;
  72. if (finalText.contains("你好")) {
  73. answer = "大家好,才是真的好!";
  74. } else if (finalText.contains("你是谁")) {
  75. answer = "我是你的小助手!";
  76. } else if (finalText.contains("天王盖地虎")) {
  77. answer = "小鸡炖蘑菇";
  78. imageId = R.drawable.m;
  79. } else if (finalText.contains("美女")) {
  80. Random random = new Random();
  81. int i = random.nextInt(mMMAnswers.length);//返回0,1,2,3
  82. int j = random.nextInt(mMMImageIDs.length);
  83. answer = mMMAnswers[i];
  84. imageId = mMMImageIDs[j];
  85. }
  86. mChatList.add(new ChatBean(answer, false, imageId));// 添加回答数据
  87. mAdapter.notifyDataSetChanged();// 刷新listview
  88. lvList.setSelection(mChatList.size() - 1);// 定位到最后一张
  89. read(answer);
  90. }
  91. }
  92. @Override
  93. public void onError(SpeechError arg0) {
  94. }
  95. };
  96.  
  97. /**
  98. * 语音朗诵
  99. */
  100. public void read(String text) {
  101. SpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer(this, null);
  102. mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan");
  103. mTts.setParameter(SpeechConstant.SPEED, "50");
  104. mTts.setParameter(SpeechConstant.VOLUME, "80");
  105. mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
  106. mTts.startSpeaking(text, null);
  107. }
  108.  
  109. /**
  110. * 开始语音识别,说完话之后开始识别。
  111. */
  112. public void startListen(View view) {
  113. RecognizerDialog iatDialog = new RecognizerDialog(this, null);
  114. // 2.设置听写参数,详见《科大讯飞MSC API手册(Android)》SpeechConstant类
  115. iatDialog.setParameter(SpeechConstant.DOMAIN, "iat");
  116. iatDialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
  117. iatDialog.setParameter(SpeechConstant.ACCENT, "mandarin");
  118. iatDialog.setListener(recognizerDialogListener);
  119. iatDialog.show();
  120. }
  121.  
  122. class ChatAdapter extends BaseAdapter {
  123. @Override
  124. public int getCount() {
  125. return mChatList.size();
  126. }
  127. @Override
  128. public ChatBean getItem(int position) {
  129. return mChatList.get(position);
  130. }
  131. @Override
  132. public long getItemId(int position) {
  133. return position;
  134. }
  135. @Override
  136. public View getView(int position, View convertView, ViewGroup parent) {
  137. ViewHolder holder;
  138. if (convertView == null) {
  139. holder = new ViewHolder();
  140. convertView = View.inflate(MainActivity.this,R.layout.list_item, null);
  141. //list_item.xml
  142. /*<?xml version="1.0" encoding="utf-8"?>
  143. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  144. android:layout_width="match_parent"
  145. android:layout_height="match_parent" >
  146.  
  147. 提问的文本框
  148. <TextView
  149. android:id="@+id/tv_ask"
  150. android:layout_width="wrap_content"
  151. android:layout_height="wrap_content"
  152. android:layout_alignParentRight="true" 居右
  153. android:layout_alignParentTop="true"
  154. android:layout_margin="5dp"
  155. android:background="@drawable/asker_bubble" 气泡图片
  156. android:gravity="center"
  157. android:text="你吃饭了吗?"
  158. android:textColor="#000"
  159. android:textSize="16sp" />
  160.  
  161. 回答的文本框
  162. <LinearLayout
  163. android:id="@+id/ll_answer"
  164. android:layout_width="wrap_content"
  165. android:layout_height="wrap_content"
  166. android:layout_below="@id/tv_ask" 回答的内容在提问的下面
  167. android:layout_margin="5dp"
  168. android:background="@drawable/answer_bubble"
  169. android:orientation="vertical" >
  170. <TextView
  171. android:id="@+id/tv_answer"
  172. android:layout_width="wrap_content"
  173. android:layout_height="wrap_content"
  174. android:layout_margin="5dp"
  175. android:text="吃个毛线啊!" 回答的内容
  176. android:textColor="#000"
  177. android:textSize="16sp" />
  178. <ImageView
  179. android:id="@+id/iv_pic"
  180. android:layout_width="wrap_content"
  181. android:layout_height="wrap_content" 回答时携带的图片
  182. android:src="@drawable/m"
  183. android:visibility="gone" />
  184. </LinearLayout>
  185. </RelativeLayout>*/
  186. holder.tvAsk = (TextView) convertView.findViewById(R.id.tv_ask);
  187. holder.tvAnswer = (TextView) convertView.findViewById(R.id.tv_answer);
  188. holder.llAnswer = (LinearLayout) convertView.findViewById(R.id.ll_answer);
  189. holder.ivPic = (ImageView) convertView.findViewById(R.id.iv_pic);
  190. convertView.setTag(holder);
  191. } else {
  192. holder = (ViewHolder) convertView.getTag();
  193. }
  194.  
  195. ChatBean item = getItem(position);
  196.  
  197. if (item.isAsker) {// 是提问者
  198. holder.tvAsk.setVisibility(View.VISIBLE);//问题显示
  199. holder.llAnswer.setVisibility(View.GONE);//回答隐藏
  200. holder.tvAsk.setText(item.text);
  201. } else {
  202. holder.tvAsk.setVisibility(View.GONE);
  203. holder.llAnswer.setVisibility(View.VISIBLE);
  204. holder.tvAnswer.setText(item.text);
  205. if (item.imageId != -1) {// 有图片
  206. holder.ivPic.setVisibility(View.VISIBLE);
  207. holder.ivPic.setImageResource(item.imageId);
  208. } else {
  209. holder.ivPic.setVisibility(View.GONE);
  210. }
  211. }
  212. return convertView;
  213. }
  214. }
  215.  
  216. static class ViewHolder {
  217. public TextView tvAsk;
  218. public TextView tvAnswer;
  219. public LinearLayout llAnswer;
  220. public ImageView ivPic;
  221. }
  222.  
  223. /**
  224. * 解析语音数据
  225. */
  226. protected String parseData(String resultString) {
  227. Gson gson = new Gson();
  228. VoiceBean bean = gson.fromJson(resultString, VoiceBean.class);
  229. /*
  230. * 语音信息封装
  231. *
  232. public class VoiceBean {
  233. public ArrayList<WSBean> ws;
  234. public class WSBean {
  235. public ArrayList<CWBean> cw;
  236. }
  237. public class CWBean {
  238. public String w;
  239. }
  240. }*/
  241. ArrayList<WSBean> ws = bean.ws;
  242. StringBuffer sb = new StringBuffer();
  243. for (WSBean wsBean : ws) {
  244. String text = wsBean.cw.get(0).w;
  245. sb.append(text);
  246. }
  247. return sb.toString();
  248. }
  249. }

json格式数据:

清单文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.itheima.voicerobot"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6.  
  7. <uses-sdk
  8. android:minSdkVersion="8"
  9. android:targetSdkVersion="17" />
  10.  
  11. <!-- 连接网络权限,用于执行云端语音能力 -->
  12. <uses-permission android:name="android.permission.INTERNET" />
  13. <!-- 获取手机录音机使用权限,听写、识别、语义理解需要用到此权限 -->
  14. <uses-permission android:name="android.permission.RECORD_AUDIO" />
  15. <!-- 读取网络信息状态 -->
  16. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  17. <!-- 获取当前wifi状态 -->
  18. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  19. <!-- 允许程序改变网络连接状态 -->
  20. <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
  21. <!-- 读取手机信息权限 -->
  22. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  23. <!-- 读取联系人权限,上传联系人需要用到此权限 -->
  24. <uses-permission android:name="android.permission.READ_CONTACTS" />
  25.  
  26. <application
  27. android:allowBackup="true"
  28. android:icon="@drawable/ic_launcher"
  29. android:label="@string/app_name"
  30. android:theme="@style/AppTheme" >
  31. <activity
  32. android:name="com.itheima.voicerobot.MainActivity"
  33. android:label="@string/app_name" >
  34. <intent-filter>
  35. <action android:name="android.intent.action.MAIN" />
  36.  
  37. <category android:name="android.intent.category.LAUNCHER" />
  38. </intent-filter>
  39. </activity>
  40. </application>
  41.  
  42. </manifest>

android129 zhihuibeijing 聊天机器人的更多相关文章

  1. 【翻译】用AIML实现的Python人工智能聊天机器人

    前言 用python的AIML包很容易就能写一个人工智能聊天机器人. AIML是Artificial Intelligence Markup Language的简写, 但它只是一个简单的XML. 下面 ...

  2. 3.C#面向对象基础聊天机器人

    基于控制台的简单版的聊天机器人,词库可以自己添加. 聊天机器人1.0版本 源码如下: using System; using System.Collections.Generic; using Sys ...

  3. Python 简易聊天机器人

    聊天机器人 | |-----MySql | |---module--"逻辑运算层" | | | |---ciku--"与词库交互" | | | |---dict ...

  4. 使用图灵机器人API实现聊天机器人

    使用图灵机器人的API需要先注册,获取key才行,这我就不说了,自己到http://www.tuling123.com/注册一个账号即可. 下面就是一个简单的python调用API实现聊天机器人的简易 ...

  5. AngularJS作出简单聊天机器人

    简单聊天机器人 很初级的对话框形式.以前做对话框使用js,今天尝试使用AngularJS做出来 这里直接使用自己写的JSON数据. <!DOCTYPE html> <html lan ...

  6. 用 AIML 开发人工智能聊天机器人

    借助 Python 的 AIML 包,我们很容易实现人工智能聊天机器人.AIML 指的是 Artificial Intelligence Markup Language (人工智能标记语言),它不过是 ...

  7. 笔记5:QQ群聊天机器人

    之前经常在别人群里看到有自动回复消息的机器人. 功能有好多,可以玩各种游戏.觉得还蛮有意思的.. 于是就去请教别人怎么弄得,但是他们都说得好复杂,好高大上,无非就是不想让别人弄 本人是个不会轻易放弃的 ...

  8. vue-miniQQ——基于Vue2实现的仿手机QQ单页面应用(接入了聊天机器人,能够进行正常对话)

    使用Vue2进行的仿手机QQ的webapp的制作,作品由个人独立开发,源码中进行了详细的注释. 由于自己也是初学Vue2,所以注释写的不够精简,请见谅. 项目地址 https://github.com ...

  9. 学习笔记TF059:自然语言处理、智能聊天机器人

    自然语言处理,语音处理.文本处理.语音识别(speech recognition),让计算机能够"听懂"人类语音,语音的文字信息"提取". 日本富国生命保险公司 ...

随机推荐

  1. Bad Request (Invalid Hostname)解决方法

    当在Windows Server 2003+IIS6做Web服务器,出现打开如http://paullevi.oicp.net,出现,Bad Request (Invalid Hostname) 的提 ...

  2. ↗☻【HTML5秘籍 #BOOK#】第1章 HTML5简介

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  3. 嵌入式Linux USB WIFI驱动的移植

    硬件平台:飞思卡尔MX258开发板 操作系统:Linux2.6.31 WIFI:    RT2860 USB WIFI模组 交叉编译环境:gcc version 4.1.2 调试步骤: 第一步:测试U ...

  4. WPF学习笔记 - 在XAML里绑定

    Binding除了默认构造函数外,还有一个可以传入Path的构造函数,下面两种方式实现的功能是一样的. <TextBlock x:Name="currentFolder" D ...

  5. 每天学点linux命令--tail,cut,sort,uniq

    tail 命令从指定点开始将文件写到标准输出.使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但刷新, ...

  6. MSP430开学的序章

    吐槽一下最近在搞什么~~~,星期三在等板子来,自己的板子,激动呀!!F5系列的板子,激动呀!结果板子到星期五才拿到!开始的时候,感觉自己没多大问题,结果一上手就问题百出,因为没仔细看用户手册,导致光盘 ...

  7. C: Answers to “The C programming language, Edition 2”

    <The C programming language> Edition 2的习题答案地址: http://users.powernet.co.uk/eton/kandr2/index.h ...

  8. ASP.NET服务器端控件(class0617)

    ASP.Net服务端基本控件介绍 ASP.Net服务端控件是ASP.Net对HTML的封装,在C#代码中就可以用txt1.Text=‘abc’这种方式来修改input的值,ASP.Net会将服务端控件 ...

  9. Codeforces Round #355 (Div. 2)

    A 弯腰 #include<cstdio> #include<cstring> #include<iostream> #include<queue> # ...

  10. Bzoj 4591: [Shoi2015]超能粒子炮·改 数论,Lucas定理,排列组合

    4591: [Shoi2015]超能粒子炮·改 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 178  Solved: 70[Submit][Stat ...