Android百度语音识别源码

Home / Android MrLee 2016-10-25 4368

百度语音搜索,基于强大的语音识别能力,支持通过语音命令快速发起搜索,让搜索更快捷、更智能。对比手机端的文本键盘输入,百度语音搜索是更自然的,符合移动设备的交互方式。 在百度强大的人工智能技术支持下,语音搜索前景广阔。语音等现代搜索引擎要做的是连接人与信息及服务,它不仅会倾听,更懂得理解与思考,未来将更好地造福人类。既然这么强大,我们就要好好的利用。

20161025151043


首先要去百度申请开发APP,然后填入appid,key,secret到你程序。有官方源码。这里我只给出最简单的使用代码
package com.voice.bdv;
import org.json.JSONObject;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.SpeechRecognizer;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
import com.baidu.speech.VoiceRecognitionService;
public class MainActivity extends Activity implements RecognitionListener {
	TextView textView;
	Button btn;
	SpeechRecognizer speechRecognizer;
	private static final int EVENT_ERROR = 11;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn = (Button) findViewById(R.id.btn);
		textView = (TextView) findViewById(R.id.log);
		speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this,
				new ComponentName(this, VoiceRecognitionService.class));
		speechRecognizer.setRecognitionListener(this);
	}
	public void onClick(View view) {
		start();
	}
	@Override
	protected void onDestroy() {
		speechRecognizer.destroy();
		super.onDestroy();
	}
	private void start() {
		textView.setText("");
		print("点击了“开始”");
		Intent intent = new Intent();
		intent.putExtra("args", "");
		speechRecognizer.startListening(intent);
	}
	@Override
	public void onReadyForSpeech(Bundle params) {
		// TODO Auto-generated method stub
		print("准备就绪,可以开始说话");
	}
	@Override
	public void onBeginningOfSpeech() {
		// TODO Auto-generated method stub
		btn.setText("说完了");
		//print("检测到用户的已经开始说话");
	}
	@Override
	public void onRmsChanged(float rmsdB) {
		// TODO Auto-generated method stub
	}
	@Override
	public void onBufferReceived(byte[] buffer) {
		// TODO Auto-generated method stub
	}
	@Override
	public void onEndOfSpeech() {
		// TODO Auto-generated method stub
		//print("检测到用户的已经停止说话");
		btn.setText("识别中");
	}
	@Override
	public void onError(int error) {
		// TODO Auto-generated method stub
	}
	@Override
	public void onResults(Bundle results) {
		// TODO Auto-generated method stub
		String json_res = results.getString("origin_result");
		try {
			print("origin_result=\n" + new JSONObject(json_res).toString(3));
		} catch (Exception e) {
			print("origin_result=[warning: bad json]\n" + json_res);
		}
		btn.setText("开始");
	}
	@Override
	public void onPartialResults(Bundle partialResults) {
		// TODO Auto-generated method stub
	}
	@Override
	public void onEvent(int eventType, Bundle params) {
		// TODO Auto-generated method stub
		switch (eventType) {
		case EVENT_ERROR:
			String reason = params.get("reason") + "";
			print("EVENT_ERROR, " + reason);
			break;
		case VoiceRecognitionService.EVENT_ENGINE_SWITCH:
			// int type = params.getInt("engine_type");
			// print("*引擎切换至" + (type == 0 ? "在线" : "离线"));
			break;
		}
	}
	private void print(String msg) {
		textView.append(msg + "\n");
		ScrollView sv = (ScrollView) textView.getParent();
		sv.smoothScrollTo(0, 1000000);
	}
}

 

本文链接:https://www.it72.com/11116.htm

推荐阅读
最新回复 (0)
返回