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

            }
        };

 

转载于:https://www.cnblogs.com/feijian/p/4519927.html

语音唤醒Android Studio中的实现过程如下: 1.在项目的build.gradle文件中添加以下依赖: ```gradle implementation 'com.iflytek:speechcloud:1.0.0' ``` 2.在AndroidManifest.xml文件中添加以下权限: ```xml <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.INTERNET" /> ``` 3.在需要使用语音唤醒的Activity中,初始化唤醒对象并设置唤醒参数: ```java // 初始化唤醒对象 private SpeechWakeuper mIvw; // 唤醒结果内容 private String resultString; // 唤醒监听器 private WakeuperListener mWakeuperListener = new WakeuperListener() { @Override public void onResult(WakeuperResult result) { try { String text = result.getResultString(); JSONObject object; object = new JSONObject(text); StringBuffer buffer = new StringBuffer(); buffer.append("【RAW】 " + text); buffer.append("\n"); buffer.append("【操作类型】" + object.optString("sst")); buffer.append("\n"); buffer.append("【唤醒词id】" + object.optString("id")); buffer.append("\n"); buffer.append("【得分】" + object.optString("score")); buffer.append("\n"); buffer.append("【前端点】" + object.optString("bos")); buffer.append("\n"); buffer.append("【尾端点】" + object.optString("eos")); resultString = buffer.toString(); } catch (JSONException e) { resultString = "结果解析出错"; e.printStackTrace(); } // 显示唤醒结果 mResultText.setText(resultString); } @Override public void onError(SpeechError error) { // 显示错误信息 mResultText.setText(error.getPlainDescription(true)); } @Override public void onBeginOfSpeech() { } @Override public void onEvent(int eventType, int isLastEvent, int arg2, Bundle obj) { } @Override public void onVolumeChanged(int volume) { } }; // 初始化唤醒对象 mIvw = SpeechWakeuper.createWakeuper(this, null); // 设置唤醒参数,详见官方文档 mIvw.setParameter(SpeechConstant.PARAMS, null); mIvw.setParameter(SpeechConstant.IVW_THRESHOLD, "0:" + curThresh); mIvw.setParameter(SpeechConstant.IVW_SST, "wakeup"); mIvw.setParameter(SpeechConstant.IVW_SHOT_WORD, "语音唤醒"); mIvw.setParameter(SpeechConstant.IVW_ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); mIvw.setParameter(SpeechConstant.IVW_RES_PATH, getResource()); mIvw.setParameter(SpeechConstant.IVW_AUDIO_PATH, getAudioResource()); mIvw.setParameter(SpeechConstant.AUDIO_FORMAT, "wav"); ``` 4.在需要启动语音唤醒的时候,调用以下方法: ```java mIvw.startListening(mWakeuperListener); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值