找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索本站精品资源

首页 › 教程频道› 小程序开发 › 查看内容

搭建https的silk录音文件语音识别服务的调用过程

作者:模板之家 2018-1-2 08:58 7247人关注

实现功能 实现一个智能生活信息查询的小秘书功能,支持查天气、新闻、日历、汇率、笑话、故事、百科、诗词、邮编、区号、菜谱、股票、节目预告,还支持闲聊、算24点、数学计算

实现功能

实现一个智能生活信息查询的小秘书功能,支持查天气、新闻、日历、汇率、笑话、故事、百科、诗词、邮编、区号、菜谱、股票、节目预告,还支持闲聊、算24点、数学计算、单位换算、购物、搜索等功能。

使用方式:

新版上线支持语音识别,按下说话,松开发送。

老版本上支持摇一摇、点界面按钮、手动输入、下拉刷新这四种方式。

扫码试用(左右皆可)

 

界面展示

 
 

开发资源

  • 免费开放语义接口平台 olami.ai
  • 微信小程序平台
  • js, css
  • 我自己搭建的https的语音识别API
接口
源码分析

这里主要介绍新版本首页相关的代码,其它部分代码在 微信小程序——智能小秘“遥知之”源码分享(语义理解基于olami)(注:这个是原来不支持语音识别的版本) 的基础上,变化不怎么大,具体可参考那篇文章。

asr.js源码:
  1. /**
  2. * 作者:happycxz
  3. * 时间:2017.09.19
  4. * 源码分享链接:http://blog.csdn.net/happycxz/article/details/78024986
  5. * 
  6. * https的silk语音识别API(专供微信小程序调用):https://api.happycxz.com/test/silk2asr/olami/asr
  7. * 该API服务搭建全过程解析及源码分享贴:http://blog.csdn.net/happycxz/article/details/78016299
  8. * 需要使用此API请联系作者QQ:404499164
  9. * 
  10. * 遵循开放、分享、自由、免费的精神,把开源坚持到底
  11. */
  12.  
  13. //获取应用实例 
  14. var app = getApp()
  15.  
  16. var UTIL = require('../../utils/util.js');
  17. var GUID = require('../../utils/GUID.js');
  18. var NLI = require('../../utils/NLI.js');
  19.  
  20. const appkey = require('../../config').appkey
  21. const appsecret = require('../../config').appsecret
  22.  
  23. //弹幕定时器
  24. var timer;
  25.  
  26. var pageSelf = undefined;
  27.  
  28. var doommList = [];
  29. class Doomm {
  30.   constructor() {
  31.     this.text = UTIL.getRandomItem(app.globalData.corpus);
  32.     this.top = Math.ceil(Math.random() * 40);
  33.     this.time = Math.ceil(Math.random() * 8 + 6);
  34.     this.color = getRandomColor();
  35.     this.display = true;
  36.     let that = this;
  37.     setTimeout(function () {
  38.       doommList.splice(doommList.indexOf(that), 1);
  39.       doommList.push(new Doomm());
  40.  
  41.       pageSelf.setData({
  42.         doommData: doommList
  43.       })
  44.     }, this.time * 1000)
  45.   }
  46. }
  47. function getRandomColor() {
  48.   let rgb = []
  49.   for (let i = 0; i < 3; ++i) {
  50.     let color = Math.floor(Math.random() * 256).toString(16)
  51.     color = color.length == 1 ? '0' + color : color
  52.     rgb.push(color)
  53.   }
  54.   return '#' + rgb.join('')
  55. }
  56.  
  57. Page({
  58.   data: {
  59.     j: 1,//帧动画初始图片 
  60.     isSpeaking: false,//是否正在说话
  61.     outputTxt : "", //输出识别结果
  62.  
  63.     doommData: []
  64.   },
  65.  
  66.   initDoomm: function () {
  67.     doommList.push(new Doomm());
  68.     doommList.push(new Doomm());
  69.     doommList.push(new Doomm());
  70.     this.setData({
  71.       doommData: doommList
  72.     })
  73.   },
  74.  
  75.   onLoad: function () {
  76.     pageSelf = this;
  77.     this.initDoomm();
  78.   },
  79.  
  80.   //手指按下 
  81.   touchdown: function () {
  82.     UTIL.log("手指按下了... new date : " + new Date)
  83.     var _this = this;
  84.     speaking.call(this);
  85.     this.setData({
  86.       isSpeaking: true
  87.     })
  88.     //开始录音 
  89.     wx.startRecord({
  90.       success: function (res) {
  91.         //临时路径,下次进入小程序时无法正常使用
  92.         var tempFilePath = res.tempFilePath;
  93.         UTIL.log('record SUCCESS file path:' + tempFilePath)
  94.         _this.setData({
  95.           recordPath: tempFilePath
  96.         });
  97.       },
  98.       fail: function (res) {
  99.         //录音失败 
  100.         wx.showModal({
  101.           title: '提示',
  102.           content: '录音的姿势不对!',
  103.           showCancel: false,
  104.           success: function (res) {
  105.             if (res.confirm) {
  106.               UTIL.log('用户点击确定')
  107.               return
  108.             }
  109.           }
  110.         })
  111.       }
  112.     })
  113.   },
  114.   //手指抬起 
  115.   touchup: function () {
  116.     UTIL.log("手指抬起了...")
  117.     this.setData({
  118.       isSpeaking: false,
  119.     })
  120.     clearInterval(this.timer)
  121.     wx.stopRecord()
  122.  
  123.     var _this = this
  124.     setTimeout(function () {
  125.       var urls = "https://api.happycxz.com/test/silk2asr/olami/asr";
  126.       UTIL.log(_this.data.recordPath);
  127.       wx.uploadFile({
  128.         url: urls,
  129.         filePath: _this.data.recordPath,
  130.         name: 'file',
  131.         formData: { "appKey": appkey, "appSecret": appsecret, "userId": UTIL.getUserUnique() },
  132.         header: { 'content-type': 'multipart/form-data' },
  133.         success: function (res) {
  134.           UTIL.log('res.data:' + res.data);
  135.  
  136.           var nliResult = getNliFromResult(res.data);
  137.           UTIL.log('nliResult:' + nliResult);
  138.           var stt = getSttFromResult(res.data);
  139.           UTIL.log('stt:' + stt);
  140.  
  141.           var sentenceResult;
  142.           try {
  143.             sentenceResult = NLI.getSentenceFromNliResult(nliResult);
  144.           } catch (e) {
  145.             UTIL.log('touchup() 错误' + e.message + '发生在' + e.lineNumber + '行');
  146.             sentenceResult = '没明白你说的,换个话题?'
  147.           }
  148.  
  149.           var lastOutput = "==>语音识别结果:\n" + stt + "\n\n==>语义处理结果:\n" + sentenceResult;
  150.           _this.setData({
  151.             outputTxt: lastOutput,
  152.           });
  153.           wx.hideToast();
  154.         },
  155.         fail: function (res) {
  156.           UTIL.log(res);
  157.           wx.showModal({
  158.             title: '提示',
  159.             content: "网络请求失败,请确保网络是否正常",
  160.             showCancel: false,
  161.             success: function (res) {
  162.             }
  163.           });
  164.           wx.hideToast();
  165.         }
  166.       });
  167.     }, 1000)
  168.   },
  169.  
  170.   //切换到老版本
  171.   turnToOld: function() {
  172.     wx.navigateTo({
  173.       url: '../index/index',
  174.     })
  175.   },
  176.  
  177. })
  178.  
  179. function getNliFromResult(res_data) {
  180.   var res_data_json = JSON.parse(res_data);
  181.   var res_data_result_json = JSON.parse(res_data_json.result);
  182.   return res_data_result_json.nli;
  183. }
  184.  
  185. function getSttFromResult(res_data) {
  186.   var res_data_json = JSON.parse(res_data);
  187.   var res_data_result_json = JSON.parse(res_data_json.result);
  188.   return res_data_result_json.asr.result;
  189. }
  190.  
  191. //麦克风帧动画 
  192. function speaking() {
  193.   var _this = this;
  194.   //话筒帧动画 
  195.   var i = 1;
  196.   this.timer = setInterval(function () {
  197.     i++;
  198.     i = i % 5;
  199.     _this.setData({
  200.       j: i
  201.     })
  202.   }, 200);
  203. }
复制代码

这部分主要实现录音按钮被按下和松开触发话筒录音及结束录音,当按钮被按下

路过

雷人

握手

鲜花

鸡蛋
原作者: 模板之家 来自: 网络收集

全部回复(0)