上屏幕界面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>
MainActivity:
package com.itheima.voicerobot; import java.util.ArrayList; import java.util.Random; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import com.google.gson.Gson; import com.iflytek.cloud.RecognizerResult; import com.iflytek.cloud.SpeechConstant; import com.iflytek.cloud.SpeechError; import com.iflytek.cloud.SpeechSynthesizer; import com.iflytek.cloud.SpeechUtility; import com.iflytek.cloud.ui.RecognizerDialog; import com.iflytek.cloud.ui.RecognizerDialogListener; import com.itheima.voicerobot.VoiceBean.CWBean; import com.itheima.voicerobot.VoiceBean.WSBean; public class MainActivity extends Activity { private ListView lvList; private ArrayList<ChatBean> mChatList = new ArrayList<ChatBean>(); /*public class ChatBean { public String text;// 内容 public boolean isAsker;// true表示提问者,否则是回答者 public int imageId = -1;// 图片id public ChatBean(String text, boolean isAsker, int imageId) { this.text = text; this.isAsker = isAsker; this.imageId = imageId; } }*/ private ChatAdapter mAdapter; private String[] mMMAnswers = new String[] { "约吗?", "讨厌!", "不要再要了!", "这是最后一张了!", "漂亮吧?" }; private int[] mMMImageIDs = new int[] { R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4 }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lvList = (ListView) findViewById(R.id.lv_list); mAdapter = new ChatAdapter(); lvList.setAdapter(mAdapter); // 初始化语音引擎 SpeechUtility.createUtility(this, SpeechConstant.APPID + "=54b8bca3"); } StringBuffer mTextBuffer = new StringBuffer(); //语音识别 private RecognizerDialogListener recognizerDialogListener = new RecognizerDialogListener() { @Override public void onResult(RecognizerResult results, boolean isLast) { // System.out.println(results.getResultString()); // System.out.println("isLast=" + isLast); String text = parseData(results.getResultString()); mTextBuffer.append(text); if (isLast) {// 会话结束 String finalText = mTextBuffer.toString(); mTextBuffer = new StringBuffer();// 清理buffer System.out.println("最终结果:" + finalText); mChatList.add(new ChatBean(finalText, true, -1)); String answer = "没听清"; int imageId = -1; if (finalText.contains("你好")) { answer = "大家好,才是真的好!"; } else if (finalText.contains("你是谁")) { answer = "我是你的小助手!"; } else if (finalText.contains("天王盖地虎")) { answer = "小鸡炖蘑菇"; imageId = R.drawable.m; } else if (finalText.contains("美女")) { Random random = new Random(); int i = random.nextInt(mMMAnswers.length);//返回0,1,2,3 int j = random.nextInt(mMMImageIDs.length); answer = mMMAnswers[i]; imageId = mMMImageIDs[j]; } mChatList.add(new ChatBean(answer, false, imageId));// 添加回答数据 mAdapter.notifyDataSetChanged();// 刷新listview lvList.setSelection(mChatList.size() - 1);// 定位到最后一张 read(answer); } } @Override public void onError(SpeechError arg0) { } }; /** * 语音朗诵 */ 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); } /** * 开始语音识别,说完话之后开始识别。 */ 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); //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>*/ 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; } /** * 解析语音数据 */ protected String parseData(String resultString) { Gson gson = new Gson(); VoiceBean bean = gson.fromJson(resultString, VoiceBean.class); /* * 语音信息封装 * public class VoiceBean { public ArrayList<WSBean> ws; public class WSBean { public ArrayList<CWBean> cw; } public class CWBean { public String w; } }*/ 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(); } }
json格式数据:
清单文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima.voicerobot" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <!-- 连接网络权限,用于执行云端语音能力 --> <uses-permission android:name="android.permission.INTERNET" /> <!-- 获取手机录音机使用权限,听写、识别、语义理解需要用到此权限 --> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <!-- 读取网络信息状态 --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- 获取当前wifi状态 --> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!-- 允许程序改变网络连接状态 --> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <!-- 读取手机信息权限 --> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!-- 读取联系人权限,上传联系人需要用到此权限 --> <uses-permission android:name="android.permission.READ_CONTACTS" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.itheima.voicerobot.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/5080242.html,如需转载请自行联系原作者