场景:进入程序后处于语音唤醒状态,当说到某个关键词的时候打开某个子界面(如:语音识别界面)

技术要点:

1、

// 设置唤醒一直保持,直到调用stopListening,传入0则完成一次唤醒后,会话立即结束(默认0)
mIvw.setParameter(SpeechConstant.KEEP_ALIVE, "1");

2、添加资源文件appid.jet 很奇怪为什么这里demo里面不需要语法文件

关键代码:

/*********************************语音唤醒************************************************/        // 语音唤醒对象        private VoiceWakeuper mIvw;        // 唤醒结果内容        private String resultString;        private void initAwake()        {            StringBuffer param = new StringBuffer();            String resPath = ResourceUtil.generateResourcePath(MainActivity.this,RESOURCE_TYPE.assets, "ivw/" + getString(R.string.app_id) + ".jet");            param.append(ResourceUtil.IVW_RES_PATH + "=" + resPath);            param.append("," + ResourceUtil.ENGINE_START + "=" + SpeechConstant.ENG_IVW);            boolean ret = SpeechUtility.getUtility().setParameter(                    ResourceUtil.ENGINE_START, param.toString());            if (!ret) {                Log.d(TAG, "启动本地引擎失败!");            }            // 初始化唤醒对象            mIvw = VoiceWakeuper.createWakeuper(this, null);                        //非空判断,防止因空指针使程序崩溃            mIvw = VoiceWakeuper.getWakeuper();            if(mIvw != null) {//                resultString = "";//                textView.setText(resultString);                // 清空参数                mIvw.setParameter(SpeechConstant.PARAMS, null);                // 唤醒门限值,根据资源携带的唤醒词个数按照“id:门限;id:门限”的格式传入                mIvw.setParameter(SpeechConstant.IVW_THRESHOLD, "0:"+ 0);  //20为门阀值                // 设置唤醒模式                mIvw.setParameter(SpeechConstant.IVW_SST, "wakeup");                // 设置唤醒一直保持,直到调用stopListening,传入0则完成一次唤醒后,会话立即结束(默认0)                  mIvw.setParameter(SpeechConstant.KEEP_ALIVE, "1");//                mIvw.startListening(mWakeuperListener); //onresume中操作            } else {                showTip("唤醒未初始化");            }        }        private WakeuperListener mWakeuperListener = new WakeuperListener() {            @Override            public void onResult(WakeuperResult result) {                try {                    String text = result.getResultString();                    resultString = text;                    JSONObject object;                    object = new JSONObject(text);                    String awakeId = object.optString("id");                    if(!StringUtil.isNullorEmpty(awakeId))                    {                        //打开语音识别界面                        boolean needShowVoiceTips = (Boolean) SPUtils.get(MainActivity.this,getResources().getString(R.string.needShowUseVoiceTips), true);                        Log.v(TAG, "IsShowVoiceTips="+needShowVoiceTips);                        mIvw = VoiceWakeuper.getWakeuper();                        if (mIvw != null) {                            mIvw.stopListening();                            mIvw = null;                        } else {                            showTip("唤醒未初始化");                        }                        if(!needShowVoiceTips)                        {                                                        if(voicePop==null||!voicePop.isShowing())                            {                                voicePop = new VoiceInputPopView(MainActivity.this);                                //显示窗口                                voicePop.showAtLocation(iv_setting, Gravity.CENTER, 0, 0); //设置layout在PopupWindow中显示的位置                            }                        }                        else                        {                            voidTipsPop= new UseVoiceTipsPop(MainActivity.this);                            //显示窗口                            voidTipsPop.showAtLocation(iv_setting, Gravity.CENTER, 0, 0); //设置layout在PopupWindow中显示的位置                        }                                            }                } catch (JSONException e) {                    resultString = "结果解析出错";                    e.printStackTrace();                }                catch (Exception e) {                    e.printStackTrace();                }//                Toast.makeText(MainActivity.this, "结果"+resultString, Toast.LENGTH_SHORT).show();            }            @Override            public void onError(SpeechError error) {                showTip(error.getPlainDescription(true));            }            @Override            public void onBeginOfSpeech() {                showTip("尝试说【讯飞语点】唤醒语音识别界面");            }            @Override            public void onEvent(int eventType, int isLast, int arg2, Bundle obj) {            }        };

更多相关文章

  1. Android(安卓)Material Design 系列之 CoordinatorLayout + Coll
  2. Android(安卓)Studio相关配置Mac版
  3. 简单实现Android绘图板
  4. android builde.gradle 动态配置域名
  5. Android(安卓)Banner轮播控件
  6. Android基础入门教程——8.1.3 Android中的13种Drawable小结 Par
  7. Android——Notification的基本使用
  8. Android(安卓)图表 MPAndroidChart 的简单使用
  9. Android(安卓)WindowManager 小结

随机推荐

  1. Android(安卓)Activity中启动另一应用程
  2. radio button修改样式
  3. 自定义VIEW③Canvas
  4. 转Launcher研究之AndroidManifest.xml分
  5. Android中的Menu点滴记录
  6. 《Android取证实战:调查、分析与移动安全
  7. Android(安卓)Retrofit OkHttp添加Interc
  8. Android源码 半小时下完Android系统源码
  9. [转]快速切换Android工程版本的方法
  10. [原创]Android在eclipse下出现Failed to