android 开发 讯飞语音唤醒功能

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

技术要点:

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

            }
        };

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。