基本信息
源码名称:语音识别:微信小程序开发示例(科大讯飞)
源码大小:3.80M
文件格式:.zip
开发语言:Java
更新时间:2019-02-26
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

微信小程序开发


本实例将会介绍微信小程序中的录音接口,音频接口,以及讯飞语音识别接口,其中,通过nodejs搭建的后台连接微信小程序和讯飞语音实现 "个人语音助理"-



// 获取录音管理器对象
const recorderManager = wx.getRecorderManager();
// 当前页面的对象
let myPage;



// 结束录音的时候自己触发
recorderManager.onStop((res) => {
  
  // 获取到了录音文件的路径
  const { tempFilePath } = res;
  // console.log(tempFilePath);

  console.log("开始发送");
  // 把录音发送到后台
  wx.uploadFile({  
    url:"http://yeah126139163.vicp.cc:33071/smart_order",
    filePath: tempFilePath,
    name:"wx_record",
    success:function(r){
      // console.log("成功",r);
      var result=JSON.parse(r.data);

      // 判断返回的状态
      if(result.code=="00000"){
        console.log(result.data);
        switch (result.data.service){
          case "weather":
          myPage.setData({iSay:result.data.text});
          myPage.setData({ xfSay: result.data.answer.text });
          sayWord(result.data.answer.text);
          break;
          default:
          break;
        }
      }else{
        // 失败
      }
    },
    fail:function(e){
      console.log("失败",e);
    }
  });

})  

function sayWord(msg){
  // console.log(msg);
  msg=msg.replace(/"/g,"");
  wx.request({
    url: `http://www.xfyun.cn/herapi/solution/synthesis?vcn=x_xiaofeng&vol=7&spd=medium&textPut=${msg}&textType=cn`,
    success:function(r){
      // console.log("文字转语音",r);
      var result=r.data;
      var url=result.data;
      myPage.audioCtx.setSrc(url);
      myPage.audioCtx.play()
    },
    fail:function(e){
      console.log(e)   
    }
  })
}


Page({

  /**
   * 页面的初始数据
   */
  data: {
    iSay:"",
    xfSay:""
  },

  // 按下录音
  start_say(){
    // console.log(1)

  // 开始录音
    recorderManager.start();
  },

  // 结束录音
  end_say(){
    // console.log(2)
    // 结束录音
    recorderManager.stop();
    
  },
  onReady: function () {
    this.audioCtx = wx.createAudioContext('myAudio');
    myPage = getCurrentPages()[0]
   
  },

})