zoukankan      html  css  js  c++  java
  • 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) {
    
                }
            };
  • 相关阅读:
    google.guava 实现 限流
    基于 Redisson 的限流 小 demo
    TX-LCN分布式事务-- TCC事务模式(消费者模块)
    TX-LCN分布式事务-- TCC事务模式(生产者模块)
    TX-LCN分布式事务-- LCN事务模式(消费者模块)
    TX-LCN分布式事务-- LCN事务模式(生产者模块)
    TX-LCN分布式事务-- LCN事务模式(eureka模块)
    TX-LCN分布式事务-- LCN事务模式(tm模块)
    TX-LCN分布式事务--学习地址和原理介绍
    LINQ to SQL系列三 使用DeferredLoadingEnabled,DataLoadOption指定加载选项
  • 原文地址:https://www.cnblogs.com/feijian/p/4519927.html
Copyright © 2011-2022 走看看