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

            }
        };

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM