实现这个功能的目的,是我看见我公司硬件工程师给客户回答问题的时候用公司研发的APP,每次都是手动输入打字,看着他带着老花镜的样子,于心不忍,毕竟咱就是干这个的.
实现效果
集成 百度语音实时识别
https://ai.baidu.com/sdk#asr
AndroidManifest.xml 文件
<uses-permission android:name="android.permission.RECORD_AUDIO" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><!-- 蓝牙录音使用,不需要可以去除 --><uses-permission android:name="android.permission.BROADCAST_STICKY" /><uses-permission android:name="android.permission.BLUETOOTH" /><uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /><uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"tools:ignore="GoogleAppIndexingWarning"><!-- 百度控制台申请的KEY --><meta-dataandroid:name="com.baidu.speech.APP_ID"android:value="22611822"/><meta-dataandroid:name="com.baidu.speech.API_KEY"android:value="YoR10GzzuZ58FYLpQ1utD5vy"/><meta-dataandroid:name="com.baidu.speech.SECRET_KEY"android:value="gGQvipUXC0dSSGmAQNMeHCMKTW4fGGrH"/><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application>
build.gradle
packagingOptions{doNotStrip "*/*/libvad.dnn.so"doNotStrip "*/*/libbd_easr_s1_merge_normal_20151216.dat.so"}
集成jar包
dependencies {
//...省略implementation files('libs\\bdasr_V3_20191210_81acdf5.jar')
}
到这里基本就可以集成了百度语音实时识别,但是这里有个坑.就是语音申请的时候得领取配额
一定要领取配额,不然一顿 4004,一开始我以为是集成错误导致了,包名检查了N次…
使用方法
这里我直接附上我写的代码了
protected TextView txtResult;private EventManager asr;protected boolean enableOffline = false; // 测试Unit 2.0 功能,必须一直联网private EditText etText;private LinearLayout llView;private CustomPopWindow mCustomPopWindowCause;private TextView tvContent;private TextView tvState;private LinearLayout llClick;/*** 基于SDK集成2.2 发送开始事件* 点击开始按钮* 测试参数填在这里*/@SuppressLint("HandlerLeak")private void start() {Map<String, Object> params = new LinkedHashMap<String, Object>();String event = null;event = SpeechConstant.ASR_START; // 替换成测试的eventif (enableOffline) {params.put(SpeechConstant.DECODER, 2);}// 基于SDK集成2.1 设置识别参数params.put(SpeechConstant.ACCEPT_AUDIO_VOLUME, false);params.put(SpeechConstant.PID, 15374); // 或 19364 Unit 2.0 固定pid,仅支持中文普通话params.put(SpeechConstant.NLU, "enable");params.put(SpeechConstant.VAD_ENDPOINT_TIMEOUT, 0); // 长语音String json = null; // 可以替换成自己的jsonjson = new JSONObject(params).toString(); // 这里可以替换成你需要测试的jsonasr.send(event, json, null, 0, 0);Log.e("X000", json + "");}/*** 点击停止按钮* 基于SDK集成4.1 发送停止事件*/private void stop() {asr.send(SpeechConstant.ASR_STOP, null, null, 0, 0); //}/*** enableOffline设为true时,在onCreate中调用* 基于SDK离线命令词1.4 加载离线资源(离线时使用)*/private void loadOfflineEngine() {Map<String, Object> params = new LinkedHashMap<String, Object>();params.put(SpeechConstant.DECODER, 2);params.put(SpeechConstant.ASR_OFFLINE_ENGINE_GRAMMER_FILE_PATH, "assets://baidu_speech_grammar.bsg");asr.send(SpeechConstant.ASR_KWS_LOAD_ENGINE, new JSONObject(params).toString(), null, 0, 0);}/*** enableOffline为true时,在onDestory中调用,与loadOfflineEngine对应* 基于SDK集成5.1 卸载离线资源步骤(离线时使用)*/private void unloadOfflineEngine() {asr.send(SpeechConstant.ASR_KWS_UNLOAD_ENGINE, null, null, 0, 0); //}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.common_mini);EventBus.getDefault().register(this);initView();initPermission();// 基于sdk集成1.1 初始化EventManager对象asr = EventManagerFactory.create(this, "asr");// 基于sdk集成1.3 注册自己的输出事件类asr.registerListener(this); // EventListener 中 onEvent方法if (enableOffline) {loadOfflineEngine(); // 测试离线命令词请开启, 测试 ASR_OFFLINE_ENGINE_GRAMMER_FILE_PATH 参数时开启}}// 基于sdk集成1.2 自定义输出事件类 EventListener 回调方法// 基于SDK集成3.1 开始回调事件@Overridepublic void onEvent(String name, String params, byte[] data, int offset, int length) {if (params != null && !params.isEmpty()) {try {JSONObject object = new JSONObject(params);JSONArray jsonArray = object.getJSONArray("results_recognition");StringBuffer str = new StringBuffer();if (jsonArray != null || jsonArray.length() != 0) {for (int i = 0; i < jsonArray.length(); i++) {String msg = jsonArray.getString(i);str.append(msg + " ");}tvContent.setText(str.toString());}} catch (JSONException e) {e.printStackTrace();}}}private void initView() {txtResult = (TextView) findViewById(R.id.txtResult);etText = findViewById(R.id.et_text);llView = findViewById(R.id.ll_layout);findViewById(R.id.btn_x).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {ShowVoice();}});}@Subscribe(threadMode = ThreadMode.MAIN)public void ConsultEvent(XYTextEvent item) {String context = item.getContext();String trim = etText.getText().toString().trim();if (!trim.isEmpty()) {etText.setText(trim + context);} else {etText.setText(context);}}/*** 展示语音*/@SuppressLint("ClickableViewAccessibility")private void ShowVoice() {final View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_windows, null);mCustomPopWindowCause = new CustomPopWindow.PopupWindowBuilder(MainActivity.this).setView(contentView).enableBackgroundDark(true) //弹出popWindow时,背景是否变暗.setBgDarkAlpha(0.6f) // 控制亮度.enableOutsideTouchableDissmiss(true).create();mCustomPopWindowCause.showAtLocation(llView, Gravity.FILL, 0, 0);tvContent = contentView.findViewById(R.id.tv_content);tvState = contentView.findViewById(R.id.tv_state);llClick = contentView.findViewById(R.id.ll_click);//触摸手势事件llClick.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:tvState.setText("正在说...~");llClick.setBackgroundResource(R.drawable.ra_bg);start();break;case MotionEvent.ACTION_UP:tvState.setText("按住说话~");llClick.setBackgroundResource(R.drawable.re_bg);stop();EventBus.getDefault().post(new XYTextEvent(tvContent.getText().toString()));mCustomPopWindowCause.dissmiss();// etText.setText(tvContent.getText().toString());break;}return true;}});}/*** android 6.0 以上需要动态申请权限*/private void initPermission() {String permissions[] = {Manifest.permission.RECORD_AUDIO,Manifest.permission.ACCESS_NETWORK_STATE,Manifest.permission.INTERNET,Manifest.permission.WRITE_EXTERNAL_STORAGE};ArrayList<String> toApplyList = new ArrayList<String>();for (String perm : permissions) {if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, perm)) {toApplyList.add(perm);// 进入到这里代表没有权限.}}String tmpList[] = new String[toApplyList.size()];if (!toApplyList.isEmpty()) {ActivityCompat.requestPermissions(this, toApplyList.toArray(tmpList), 123);}}@Overridepublic void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {// 此处为android 6.0以上动态授权的回调,用户自行实现。}@Overrideprotected void onPause() {super.onPause();asr.send(SpeechConstant.ASR_CANCEL, "{}", null, 0, 0);}@Overrideprotected void onDestroy() {super.onDestroy();// 基于SDK集成4.2 发送取消事件asr.send(SpeechConstant.ASR_CANCEL, "{}", null, 0, 0);if (enableOffline) {unloadOfflineEngine(); // 测试离线命令词请开启, 测试 ASR_OFFLINE_ENGINE_GRAMMER_FILE_PATH 参数时开启}// 基于SDK集成5.2 退出事件管理器// 必须与registerListener成对出现,否则可能造成内存泄露asr.unregisterListener(this);if (EventBus.getDefault().isRegistered(this))EventBus.getDefault().unregister(this);}
附带三方库
implementation 'com.github.pinguo-zhouwei:CustomPopwindow:2.1.1'implementation 'org.greenrobot:eventbus:3.1.1'
后记 :功能实现了,老工程师很开心.他跟我说他之所以打字,是因为他在给客户回答问题的时候得思索一下…
至于语音输入功能,他说现在的输入法都有这个功能了.要不你打字的时候,试一下长按空格键…-_-||