Android语音识别

2022-01-12 14:27:10 浏览数 (3)

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

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

代码语言:javascript复制
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.主布局

代码语言:javascript复制
<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.子布局

代码语言:javascript复制
<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.代码

聊天信息对象

代码语言:javascript复制
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数据)

代码语言:javascript复制
public class VoiceBean {
    public ArrayList<WSBean> ws;
    public class WSBean {
        public ArrayList<CWBean> cw;
    }
    public class CWBean {
        public String w;
    }
}
 

mainactivity:

代码语言:javascript复制
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 人点赞