16.语音识别

语音识别 - 科大讯飞 开放平台 http://open.voicecloud.cn/

需要拷贝lib、assets、并在清单文件中写一些权限

public class MainActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 初始化语音引擎
		SpeechUtility.createUtility(this, SpeechConstant.APPID + "=54b8bca3");
	}
	/**
	 * 开始听写
	 * 
	 * @param view
	 */
	public void listen(View view) {
		// 1.创建SpeechRecognizer对象,第二个参数:本地听写时传InitListener
		SpeechRecognizer mIat = SpeechRecognizer.createRecognizer(this, null);
		// 2.设置听写参数,详见《科大讯飞MSC API手册(Android)》SpeechConstant类
		mIat.setParameter(SpeechConstant.DOMAIN, "iat");
		mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
		mIat.setParameter(SpeechConstant.ACCENT, "mandarin");
		// 开始听写
		mIat.startListening(mRecoListener);
	}
	/**
	 * 交互动画
	 * 
	 * @param view
	 */
	public void listenUI(View view) {
		RecognizerDialog iatDialog = new RecognizerDialog(this, mInitListener);
		// 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();
	}
	/**
	 * 语音朗诵
	 */
	public void read(View view) {
		SpeechSynthesizer mTts = SpeechSynthesizer
				.createSynthesizer(this, null);
		mTts.setParameter(SpeechConstant.VOICE_NAME, "vixm");
		mTts.setParameter(SpeechConstant.SPEED, "50");
		mTts.setParameter(SpeechConstant.VOLUME, "80");
		mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
		mTts.startSpeaking("床前明月光,地上鞋两双,床上狗男女,其中就有你! 你好啊?",
				mSynthesizerListener);
	}
	private SynthesizerListener mSynthesizerListener = new SynthesizerListener() {
		@Override
		public void onSpeakResumed() {
			// TODO Auto-generated method stub
		}
		@Override
		public void onSpeakProgress(int arg0, int arg1, int arg2) {
			// TODO Auto-generated method stub
		}
		@Override
		public void onSpeakPaused() {
			// TODO Auto-generated method stub
		}
		@Override
		public void onSpeakBegin() {
			// TODO Auto-generated method stub
		}
		@Override
		public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
			// TODO Auto-generated method stub
		}
		@Override
		public void onCompleted(SpeechError arg0) {
			// TODO Auto-generated method stub
		}
		@Override
		public void onBufferProgress(int arg0, int arg1, int arg2, String arg3) {
			// TODO Auto-generated method stub
		}
	};
	private RecognizerDialogListener recognizerDialogListener = new RecognizerDialogListener() {
		@Override
		public void onResult(RecognizerResult results, boolean isLast) {
			System.out.println(results.getResultString());
			System.out.println("isLast=" + isLast);
		}
		@Override
		public void onError(SpeechError arg0) {
		}
	};
	private InitListener mInitListener = new InitListener() {
		@Override
		public void onInit(int arg0) {
		}
	};
	private RecognizerListener mRecoListener = new RecognizerListener() {
		/**
		 * 语音识别结果 isLast=true表示会话结束
		 */
		@Override
		public void onResult(RecognizerResult results, boolean isLast) {
			System.out.println(results.getResultString());
		}
		@Override
		public void onBeginOfSpeech() {
			// TODO Auto-generated method stub
		}
		@Override
		public void onEndOfSpeech() {
			// TODO Auto-generated method stub
		}
		@Override
		public void onError(SpeechError arg0) {
			// TODO Auto-generated method stub
		}
		@Override
		public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
			// TODO Auto-generated method stub
		}
		@Override
		public void onVolumeChanged(int arg0) {
			// TODO Auto-generated method stub
		}
	};
}

聊天机器人

1.主布局

<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>

2.子布局

<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>

3.代码

聊天信息对象

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;
	}
}

语音信息封装类(需要解析json数据)

  1. public class VoiceBean { public ArrayList<WSBean> ws; public class WSBean { public ArrayList<CWBean> cw; } public class CWBean { public String w; } }

mainactivity:

public class MainActivity extends Activity {
	private ListView lvList;
	private ArrayList<ChatBean> mChatList = new ArrayList<ChatBean>();
	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);
					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);
	}
	/**
	 * 开始语音识别
	 * 
	 * @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();
	}
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Hongten

android开发_Intent高级学习(保存Activity状态)

http://www.cnblogs.com/hongten/gallery/image/112159.html

1032
来自专栏图像识别与深度学习

《Android》Lesson13-ListView

23111
来自专栏非著名程序员

Android学习笔记(一)之仿正点闹钟时间齿轮滑动的效果

看到正点闹钟上的设置时间的滑动效果非常好看,自己就想做一个那样的,在网上就开始搜资料了,看到网上有的齿轮效果的代码非常多,也非常难懂,我就决定自己研究一下,现在...

23610
来自专栏向治洪

android drawable

android.graphics.drawable.Drawable是个虚类。 它的直接子类有 BitmapDrawable, ClipDrawable, Co...

21010
来自专栏cmazxiaoma的架构师之路

Android仿淘宝购物车,玩转电商购物车

1783
来自专栏GIS讲堂

安卓底部选项卡

腾讯QQ有了更新,在自己的手机上更新了一下,试运行了一段时间,被底部的选项卡给深深的迷住了。于是,有了去做做它的原始冲动与欲望,在历经一个早上的琢磨,终于实现了...

2332
来自专栏木头编程 - moTzxx

博客园客户端设计

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011415782/article/de...

964
来自专栏项勇

笔记41 | Android加载器Adapter的几个练习

1273
来自专栏向治洪

popupwindow和listview

在使用PopupWindow的时候,有一个不好的地方就是不太好设置弹出窗体的大小。如果指定绝对大小,那么对于不同分辨率不同尺寸的手机来说,显示出来效果会不同,...

25210
来自专栏james大数据架构

Activity间中使用Intent传值

主页面用来输入一个值传入第二个页面显示,关闭第二个页面返回一个值 主页布局: <RelativeLayout xmlns:android="http://sch...

19810

扫码关注云+社区

领取腾讯云代金券