代码改变世界

腾讯语音输入踩的那些坑

2024-03-12 09:29  雄风狂飙  阅读(57)  评论(0)    收藏  举报

使用腾讯的ars进行语音输入的时候,又很多坑

1.如果申请使用腾讯的插件,如果是个人小程序,可能会出现“89260”的错误。

解决方法是:

在小程序中引入ars.js,直接使用,

对应核心代码如下:

  <view class="identify-text">{{result}}</view>
  <button type="primary" size="default" bindtap='startLy' class="btn btn-start" wx:if="{{!recording}}">开始识别</button>
<button type="primary" size="default" bindtap='endLy'class="btn btn-end" wx:if="{{recording}}">停止识别</button>

  

onLoad() {


    speechRecognizerManager.OnSentenceEnd = (res) => {
        console.log('一句话结束', res);
        resultText += res.result.voice_text_str;
        console.log("resultText",resultText);
        this.setData({
            textResult: resultText
        });
      };
      speechRecognizerManager.OnRecognitionComplete = (res) => {
        console.log('识别结束', res);
        
        this.isRecognizeStop = true;
        this.setData({
          recording: false,
          disabled: false
        });
      };
      // 识别错误
      speechRecognizerManager.OnError = (res) => {
        console.log("识别错误",res);
        this.isCanSendData = false;
        this.setData({
          recording: false,
          disabled: false
        });
      };
      speechRecognizerManager.OnRecorderStop = () => {
        console.log('超过录音时长');
      };
    

  }

startLy:  async function(e){
    resultText='';
  const that = this;
  wx.getSetting({
    success(res) {
      if (!res.authSetting['scope.record']) {
        wx.authorize({
          scope: 'scope.record',
          success() {
            // 用户已经同意小程序使用录音功能,后续调用 record 接口不会弹窗询问
           that.startAsr();
          }, fail() {
            wx.showToast({ title: '未获取录音权限', icon: 'none' });
            // console.log("fail auth")
          }
        });
      } else {
        //self.startAsr();
           that.startAsr();
        // console.log("record has been authed")
      }
    }, fail(res) {
      // console.log("fail",res)
    }
  });
},
startAsr: function() {
  wx.showToast({
    title: '建立连接中',
    icon: 'none'
  });
  resultText = '';
  const params = {
    // 用户参数
    secretkey: '',//这个需要申请
    secretid:  '',
    appid: 0,//这个不是小程序的appid,而是你的账号appid
    // 录音参数
    // duration: 100000,
    // frameSize: 1.28,  //单位:k

    // 实时识别接口参数
    engine_model_type : '16k_zh',
    // 以下为非必填参数,可跟据业务自行修改
    // hotword_id : '08003a00000000000000000000000000',
    needvad: 1,
    // filter_dirty: 1,
    // filter_modal: 2,
    filter_punc: 1,
    // convert_num_mode : 1,
    word_info: 2,
    vad_silence_time: 200
  };
  
  this.setData({recording:true});

  speechRecognizerManager.start(params);

  wx.vibrateShort();
},
endLy: function(e){
  this.setData({
    recording: false,
    disabled: false
  });
  speechRecognizerManager.stop();
},

js中引入的地方和全局变量

const ars = require('./asr.min');
const speechRecognizerManager =  ars.getRecorderSpeechRecognizer();

  

  

2.写好代码,发现怎么都识别不了

第一个可能是appid,你填成了自己申请的小程序的appid,这里需要的是你的账号的appid

第二个是,我自己测试在真机可以调试输入语音成功,但是在本机是不行的。