码迷,mamicode.com
首页 > 其他好文 > 详细

16.语音机器人、二维码

时间:2015-10-29 09:34:40      阅读:394      评论:0      收藏:0      [点我收藏+]

标签:

语音识别

科大讯飞 开放平台 http://open.voicecloud.cn/
需要拷贝lib、assets、并在清单文件中写一些权限
-二维码见附件,直接引用
  1. public class MainActivity extends Activity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. // 初始化语音引擎
  7. SpeechUtility.createUtility(this, SpeechConstant.APPID + "=54b8bca3");
  8. }
  9. /**
  10. * 开始听写
  11. *
  12. * @param view
  13. */
  14. public void listen(View view) {
  15. // 1.创建SpeechRecognizer对象,第二个参数:本地听写时传InitListener
  16. SpeechRecognizer mIat = SpeechRecognizer.createRecognizer(this, null);
  17. // 2.设置听写参数,详见《科大讯飞MSC API手册(Android)》SpeechConstant类
  18. mIat.setParameter(SpeechConstant.DOMAIN, "iat");
  19. mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
  20. mIat.setParameter(SpeechConstant.ACCENT, "mandarin");
  21. // 开始听写
  22. mIat.startListening(mRecoListener);
  23. }
  24. /**
  25. * 交互动画
  26. *
  27. * @param view
  28. */
  29. public void listenUI(View view) {
  30. RecognizerDialog iatDialog = new RecognizerDialog(this, mInitListener);
  31. // 2.设置听写参数,详见《科大讯飞MSC API手册(Android)》SpeechConstant类
  32. iatDialog.setParameter(SpeechConstant.DOMAIN, "iat");
  33. iatDialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
  34. iatDialog.setParameter(SpeechConstant.ACCENT, "mandarin");
  35. iatDialog.setListener(recognizerDialogListener);
  36. iatDialog.show();
  37. }
  38. /**
  39. * 语音朗诵
  40. */
  41. public void read(View view) {
  42. SpeechSynthesizer mTts = SpeechSynthesizer
  43. .createSynthesizer(this, null);
  44. mTts.setParameter(SpeechConstant.VOICE_NAME, "vixm");
  45. mTts.setParameter(SpeechConstant.SPEED, "50");
  46. mTts.setParameter(SpeechConstant.VOLUME, "80");
  47. mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
  48. mTts.startSpeaking("床前明月光,地上鞋两双,床上狗男女,其中就有你! 你好啊?",
  49. mSynthesizerListener);
  50. }
  51. private SynthesizerListener mSynthesizerListener = new SynthesizerListener() {
  52. @Override
  53. public void onSpeakResumed() {
  54. // TODO Auto-generated method stub
  55. }
  56. @Override
  57. public void onSpeakProgress(int arg0, int arg1, int arg2) {
  58. // TODO Auto-generated method stub
  59. }
  60. @Override
  61. public void onSpeakPaused() {
  62. // TODO Auto-generated method stub
  63. }
  64. @Override
  65. public void onSpeakBegin() {
  66. // TODO Auto-generated method stub
  67. }
  68. @Override
  69. public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
  70. // TODO Auto-generated method stub
  71. }
  72. @Override
  73. public void onCompleted(SpeechError arg0) {
  74. // TODO Auto-generated method stub
  75. }
  76. @Override
  77. public void onBufferProgress(int arg0, int arg1, int arg2, String arg3) {
  78. // TODO Auto-generated method stub
  79. }
  80. };
  81. private RecognizerDialogListener recognizerDialogListener = new RecognizerDialogListener() {
  82. @Override
  83. public void onResult(RecognizerResult results, boolean isLast) {
  84. System.out.println(results.getResultString());
  85. System.out.println("isLast=" + isLast);
  86. }
  87. @Override
  88. public void onError(SpeechError arg0) {
  89. }
  90. };
  91. private InitListener mInitListener = new InitListener() {
  92. @Override
  93. public void onInit(int arg0) {
  94. }
  95. };
  96. private RecognizerListener mRecoListener = new RecognizerListener() {
  97. /**
  98. * 语音识别结果 isLast=true表示会话结束
  99. */
  100. @Override
  101. public void onResult(RecognizerResult results, boolean isLast) {
  102. System.out.println(results.getResultString());
  103. }
  104. @Override
  105. public void onBeginOfSpeech() {
  106. // TODO Auto-generated method stub
  107. }
  108. @Override
  109. public void onEndOfSpeech() {
  110. // TODO Auto-generated method stub
  111. }
  112. @Override
  113. public void onError(SpeechError arg0) {
  114. // TODO Auto-generated method stub
  115. }
  116. @Override
  117. public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
  118. // TODO Auto-generated method stub
  119. }
  120. @Override
  121. public void onVolumeChanged(int arg0) {
  122. // TODO Auto-generated method stub
  123. }
  124. };
  125. }

聊天机器人
1.主布局
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <ListView
  7. android:id="@+id/lv_list"
  8. android:layout_width="wrap_content"
  9. android:layout_height="0dp"
  10. android:divider="@null"
  11. android:layout_weight="1" />
  12. <LinearLayout
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:background="@drawable/bottom_bar"
  16. android:gravity="center"
  17. android:orientation="vertical" >
  18. <Button
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:layout_margin="10dp"
  22. android:background="@drawable/btn_selector"
  23. android:onClick="startListen"
  24. android:text="点击开始语音识别"
  25. android:textColor="#000"
  26. android:textSize="16sp" />
  27. </LinearLayout>
  28. </LinearLayout>
2.子布局
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent" >
  4. <TextView
  5. android:id="@+id/tv_ask"
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:layout_alignParentRight="true"
  9. android:layout_alignParentTop="true"
  10. android:layout_margin="5dp"
  11. android:background="@drawable/asker_bubble"
  12. android:gravity="center"
  13. android:text="你吃饭了吗?"
  14. android:textColor="#000"
  15. android:textSize="16sp" />
  16. <LinearLayout
  17. android:id="@+id/ll_answer"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:layout_below="@id/tv_ask"
  21. android:layout_margin="5dp"
  22. android:background="@drawable/answer_bubble"
  23. android:orientation="vertical" >
  24. <TextView
  25. android:id="@+id/tv_answer"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:layout_margin="5dp"
  29. android:text="吃个毛线啊!"
  30. android:textColor="#000"
  31. android:textSize="16sp" />
  32. <ImageView
  33. android:id="@+id/iv_pic"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:src="@drawable/m"
  37. android:visibility="gone" />
  38. </LinearLayout>
  39. </RelativeLayout>
3.代码
聊天信息对象
  1. public class ChatBean {
  2. public String text;// 内容
  3. public boolean isAsker;// true表示提问者,否则是回答者
  4. public int imageId = -1;// 图片id
  5. public ChatBean(String text, boolean isAsker, int imageId) {
  6. this.text = text;
  7. this.isAsker = isAsker;
  8. this.imageId = imageId;
  9. }
  10. }
语音信息封装类(需要解析json数据)
  1. public class VoiceBean {
  2. public ArrayList<WSBean> ws;
  3. public class WSBean {
  4. public ArrayList<CWBean> cw;
  5. }
  6. public class CWBean {
  7. public String w;
  8. }
  9. }
mainactivity:
  1. public class MainActivity extends Activity {
  2. private ListView lvList;
  3. private ArrayList<ChatBean> mChatList = new ArrayList<ChatBean>();
  4. private ChatAdapter mAdapter;
  5. private String[] mMMAnswers = new String[] { "约吗?", "讨厌!", "不要再要了!",
  6. "这是最后一张了!", "漂亮吧?" };
  7. private int[] mMMImageIDs = new int[] { R.drawable.p1, R.drawable.p2,
  8. R.drawable.p3, R.drawable.p4 };
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. lvList = (ListView) findViewById(R.id.lv_list);
  14. mAdapter = new ChatAdapter();
  15. lvList.setAdapter(mAdapter);
  16. // 初始化语音引擎
  17. SpeechUtility.createUtility(this, SpeechConstant.APPID + "=54b8bca3");
  18. }
  19. StringBuffer mTextBuffer = new StringBuffer();
  20. private RecognizerDialogListener recognizerDialogListener = new RecognizerDialogListener() {
  21. @Override
  22. public void onResult(RecognizerResult results, boolean isLast) {
  23. // System.out.println(results.getResultString());
  24. // System.out.println("isLast=" + isLast);
  25. String text = parseData(results.getResultString());
  26. mTextBuffer.append(text);
  27. if (isLast) {// 会话结束
  28. String finalText = mTextBuffer.toString();
  29. mTextBuffer = new StringBuffer();// 清理buffer
  30. System.out.println("最终结果:" + finalText);
  31. mChatList.add(new ChatBean(finalText, true, -1));
  32. String answer = "没听清";
  33. int imageId = -1;
  34. if (finalText.contains("你好")) {
  35. answer = "大家好,才是真的好!";
  36. } else if (finalText.contains("你是谁")) {
  37. answer = "我是你的小助手!";
  38. } else if (finalText.contains("天王盖地虎")) {
  39. answer = "小鸡炖蘑菇";
  40. imageId = R.drawable.m;
  41. } else if (finalText.contains("美女")) {
  42. Random random = new Random();
  43. int i = random.nextInt(mMMAnswers.length);
  44. int j = random.nextInt(mMMImageIDs.length);
  45. answer = mMMAnswers[i];
  46. imageId = mMMImageIDs[j];
  47. }
  48. mChatList.add(new ChatBean(answer, false, imageId));// 添加回答数据
  49. mAdapter.notifyDataSetChanged();// 刷新listview
  50. lvList.setSelection(mChatList.size() - 1);// 定位到最后一张
  51. read(answer);
  52. }
  53. }
  54. @Override
  55. public void onError(SpeechError arg0) {
  56. }
  57. };
  58. /**
  59. * 语音朗诵
  60. */
  61. public void read(String text) {
  62. SpeechSynthesizer mTts = SpeechSynthesizer
  63. .createSynthesizer(this, null);
  64. mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan");
  65. mTts.setParameter(SpeechConstant.SPEED, "50");
  66. mTts.setParameter(SpeechConstant.VOLUME, "80");
  67. mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
  68. mTts.startSpeaking(text, null);
  69. }
  70. /**
  71. * 开始语音识别
  72. *
  73. * @param view
  74. */
  75. public void startListen(View view) {
  76. RecognizerDialog iatDialog = new RecognizerDialog(this, null);
  77. // 2.设置听写参数,详见《科大讯飞MSC API手册(Android)》SpeechConstant类
  78. iatDialog.setParameter(SpeechConstant.DOMAIN, "iat");
  79. iatDialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
  80. iatDialog.setParameter(SpeechConstant.ACCENT, "mandarin");
  81. iatDialog.setListener(recognizerDialogListener);
  82. iatDialog.show();
  83. }
  84. class ChatAdapter extends BaseAdapter {
  85. @Override
  86. public int getCount() {
  87. return mChatList.size();
  88. }
  89. @Override
  90. public ChatBean getItem(int position) {
  91. return mChatList.get(position);
  92. }
  93. @Override
  94. public long getItemId(int position) {
  95. return position;
  96. }
  97. @Override
  98. public View getView(int position, View convertView, ViewGroup parent) {
  99. ViewHolder holder;
  100. if (convertView == null) {
  101. holder = new ViewHolder();
  102. convertView = View.inflate(MainActivity.this,
  103. R.layout.list_item, null);
  104. holder.tvAsk = (TextView) convertView.findViewById(R.id.tv_ask);
  105. holder.tvAnswer = (TextView) convertView
  106. .findViewById(R.id.tv_answer);
  107. holder.llAnswer = (LinearLayout) convertView
  108. .findViewById(R.id.ll_answer);
  109. holder.ivPic = (ImageView) convertView
  110. .findViewById(R.id.iv_pic);
  111. convertView.setTag(holder);
  112. } else {
  113. holder = (ViewHolder) convertView.getTag();
  114. }
  115. ChatBean item = getItem(position);
  116. if (item.isAsker) {// 是提问者
  117. holder.tvAsk.setVisibility(View.VISIBLE);
  118. holder.llAnswer.setVisibility(View.GONE);
  119. holder.tvAsk.setText(item.text);
  120. } else {
  121. holder.tvAsk.setVisibility(View.GONE);
  122. holder.llAnswer.setVisibility(View.VISIBLE);
  123. holder.tvAnswer.setText(item.text);
  124. if (item.imageId != -1) {// 有图片
  125. holder.ivPic.setVisibility(View.VISIBLE);
  126. holder.ivPic.setImageResource(item.imageId);
  127. } else {
  128. holder.ivPic.setVisibility(View.GONE);
  129. }
  130. }
  131. return convertView;
  132. }
  133. }
  134. static class ViewHolder {
  135. public TextView tvAsk;
  136. public TextView tvAnswer;
  137. public LinearLayout llAnswer;
  138. public ImageView ivPic;
  139. }
  140. /**
  141. * 解析语音数据
  142. *
  143. * @param resultString
  144. */
  145. protected String parseData(String resultString) {
  146. Gson gson = new Gson();
  147. VoiceBean bean = gson.fromJson(resultString, VoiceBean.class);
  148. ArrayList<WSBean> ws = bean.ws;
  149. StringBuffer sb = new StringBuffer();
  150. for (WSBean wsBean : ws) {
  151. String text = wsBean.cw.get(0).w;
  152. sb.append(text);
  153. }
  154. return sb.toString();
  155. }
  156. }











附件列表

     

    16.语音机器人、二维码

    标签:

    原文地址:http://www.cnblogs.com/liuyu0529/p/4919365.html

    (0)
    (0)
       
    举报
    评论 一句话评论(0
    登录后才能评论!
    © 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
    迷上了代码!