小程序 wx.getRecorderManager 录音 to 语音识别
微信扫小程序码看调用效果(自然语言理解小助手)
欢迎转载,请保留原文链接:http://www.happycxz.com/m/?p=125
这次主要是把我的api更新了一下,支持微信小程序新的录音接口,录出来的是mp3格式。顺便更新一下我的微信小程序“遥知之”,用上新录音接口 wx.getRecorderManager 。
这里只贴上关键代码:
//获取应用实例
var app = getApp()
var UTIL = require('../../utils/util.js');
var GUID = require('../../utils/GUID.js');
var NLI = require('../../utils/NLI.js');
const appkey = require('../../config').appkey
const appsecret = require('../../config').appsecret
//微信小程序新录音接口,录出来的是aac或者mp3,这里要录成mp3
const mp3Recorder = wx.getRecorderManager()
const mp3RecoderOptions = {
duration: 60000,
sampleRate: 16000,
numberOfChannels: 1,
encodeBitRate: 48000,
format: 'mp3',
//frameSize: 50
}
//弹幕定时器
var timer;
var pageSelf = undefined;
var doommList = [];
class Doomm {
constructor() {
this.text = UTIL.getRandomItem(app.globalData.corpus);
this.top = Math.ceil(Math.random() * 40);
this.time = Math.ceil(Math.random() * 8 + 6);
this.color = getRandomColor();
this.display = true;
let that = this;
setTimeout(function () {
doommList.splice(doommList.indexOf(that), 1);
doommList.push(new Doomm());
pageSelf.setData({
doommData: doommList
})
}, this.time * 1000)
}
}
function getRandomColor() {
let rgb = []
for (let i = 0; i < 3; ++i) {
let color = Math.floor(Math.random() * 256).toString(16)
color = color.length == 1 ? '0' + color : color
rgb.push(color)
}
return '#' + rgb.join('')
}
Page({
data: {
j: 1,//帧动画初始图片
isSpeaking: false,//是否正在说话
outputTxt : "", //输出识别结果
doommData: []
},
initDoomm: function () {
doommList.push(new Doomm());
doommList.push(new Doomm());
doommList.push(new Doomm());
this.setData({
doommData: doommList
})
},
onLoad: function () {
pageSelf = this;
this.initDoomm();
//onLoad中为录音接口注册两个回调函数,主要是onStop,拿到录音mp3文件的文件名(不用在意文件后辍是.dat还是.mp3,后辍不决定音频格式)
mp3Recorder.onStart(() => {
UTIL.log('mp3Recorder.onStart()...')
})
mp3Recorder.onStop((res) => {
UTIL.log('mp3Recorder.onStop() ' + res)
const { tempFilePath } = res
var urls = "https://api.happycxz.com/wxapp/mp32asr";
UTIL.log('mp3Recorder.onStop() tempFilePath:' + tempFilePath)
processFileUploadForAsr(urls, tempFilePath, this);
})
},
/////////////////////////////////////////////////////////////// 以下是调用新接口实现的录音,录出来的是 mp3
touchdown: function () {
//touchdown_mp3: function () {
UTIL.log("mp3Recorder.start with" + mp3RecoderOptions)
var _this = this;
speaking.call(this);
this.setData({
isSpeaking: true
})
mp3Recorder.start(mp3RecoderOptions);
},
touchup: function () {
//touchup_mp3: function () {
UTIL.log("mp3Recorder.stop")
this.setData({
isSpeaking: false,
})
mp3Recorder.stop();
},
//切换到老版本
turnToOld: function () {
wx.navigateTo({
url: '../index/index',
})
},
/////////////////////////////////////////////////////////////// 以下是调用老接口实现的录音,录出来的是 silk_v3
//手指按下
touchdown_silk: function () {
//touchdown: function () {
UTIL.log("手指按下了... new date : " + new Date)
var _this = this;
speaking.call(this);
this.setData({
isSpeaking: true
})
//开始录音
wx.startRecord({
success: function (res) {
//临时路径,下次进入小程序时无法正常使用
var tempFilePath = res.tempFilePath;
UTIL.log('record SUCCESS file path:' + tempFilePath)
_this.setData({
recordPath: tempFilePath
});
},
fail: function (res) {
//录音失败
wx.showModal({
title: '提示',
content: '录音的姿势不对!',
showCancel: false,
success: function (res) {
if (res.confirm) {
UTIL.log('用户点击确定')
return
}
}
})
}
})
},
//手指抬起
touchup_silk: function () {
//touchup: function () {
UTIL.log("手指抬起了...")
this.setData({
isSpeaking: false,
})
clearInterval(this.timer)
wx.stopRecord()
var _this = this
setTimeout(function () {
var urls = "https://api.happycxz.com/wxapp/silk2asr/";
UTIL.log(_this.data.recordPath);
processFileUploadForAsr(urls, _this.data.recordPath, _this);
}, 1000)
},
})
//上传录音文件到 api.happycxz.com 接口,处理语音识别和语义,结果输出到界面
function processFileUploadForAsr(urls, filePath, _this) {
wx.uploadFile({
url: urls,
filePath: filePath,
name: 'file',
formData: { "appKey": appkey, "appSecret": appsecret, "userId": UTIL.getUserUnique() },
header: { 'content-type': 'multipart/form-data' },
success: function (res) {
UTIL.log('res.data:' + res.data);
var nliResult = getNliFromResult(res.data);
UTIL.log('nliResult:' + nliResult);
var stt = getSttFromResult(res.data);
UTIL.log('stt:' + stt);
var sentenceResult;
try {
sentenceResult = NLI.getSentenceFromNliResult(nliResult);
} catch (e) {
UTIL.log('touchup() 错误' + e.message + '发生在' + e.lineNumber + '行');
sentenceResult = '没明白你说的,换个话题?'
}
var lastOutput = "==>语音识别结果:\n" + stt + "\n\n==>语义处理结果:\n" + sentenceResult;
_this.setData({
outputTxt: lastOutput,
});
wx.hideToast();
},
fail: function (res) {
UTIL.log(res);
wx.showModal({
title: '提示',
content: "网络请求失败,请确保网络是否正常",
showCancel: false,
success: function (res) {
}
});
wx.hideToast();
}
});
}
function getNliFromResult(res_data) {
var res_data_json = JSON.parse(res_data);
var res_data_result_json = JSON.parse(res_data_json.result);
return res_data_result_json.nli;
}
function getSttFromResult(res_data) {
var res_data_json = JSON.parse(res_data);
var res_data_result_json = JSON.parse(res_data_json.result);
return res_data_result_json.asr.result;
}
//麦克风帧动画
function speaking() {
var _this = this;
//话筒帧动画
var i = 1;
this.timer = setInterval(function () {
i++;
i = i % 5;
_this.setData({
j: i
})
}, 200);
}
主要调用API的地方是:processFileUploadForAsr()。
代码中保留了 touchdown_silk 和 touchup_silk 是调用小程序提供的老录音接口录silk的,可以参考,换老接口也是可以用的。
与“遥知之”相关的博客文章主要在我的CSDN博客中:
http://blog.csdn.net/happycxz/article/details/78024986
该API服务搭建全过程解析及源码分享贴:
http://blog.csdn.net/happycxz/article/details/78016299
这次的代码更新,托管在github上,感兴趣的可以关注:
api服务器端github项目
小程序端github项目
小程序 wx.getRecorderManager 录音 to 语音识别的更多相关文章
- 小程序wx.getRecorderManager()录音管理
小程序中提供了两种录音的API,wx.startRecord和wx.getRecorderManager(),前一个现在微信团队已经不再维护,所以在这里写一下新的录音管理,比之前要强大 1.小程序录音 ...
- 微信小程序wx.request接口
微信小程序wx.request接口 wx.request是小程序客户端与服务器端交互的接口 HTTPS 请求 一个微信小程序,只能同时(同时不能大于5个)有5个网络请求 wx.request(OBJE ...
- ***小程序wx.getUserInfo不能弹出授权窗口后的解决方案
微信更新api后,wx.getUserInfo在开发和体验版本都不能弹出授权窗口.微信文档说明: 注意:此接口有调整,使用该接口将不再出现授权弹窗,请使用 <button open-type=& ...
- 监控微信小程序wx.request请求失败
在微信小程序里,与后台服务器交互的主要接口函数是wx.request(),用于发起 HTTPS 网络请求.其重要性不言而喻.然而,却经常遇到请求失败的问题,笔者特意谷歌"wx.request ...
- 小程序wx.showToast()方法实现文字换行
小程序wx.showToast()方法实现文字换行 在文字中间加上 '\r\n' 真机中生效 wx.showToast({ title: `换行前内容\r\n换行后内容`, icon: 'none' ...
- 微信小程序wx.showActionSheet调用客服信息功能
微信小程序wx.showActionSheet调用客服消息功能 官方文档的代码: wx.showActionSheet({ itemList: ['A', 'B', 'C'], success (re ...
- 微信小程序-图片、录音、音频播放、音乐播放、视屏、文件
图片: wx.chooseImage(OBJECT) 从本地相册选择图片或使用相机拍照. OBJECT参数说明: 注:文件的临时路径,在小程序本次启动期间可以正常使用,如需持久保存,需在主动调用 wx ...
- 微信小程序语音与讯飞语音识别接口(Java)
项目需求,需要使用讯飞的语音识别接口,将微信小程序上传的录音文件识别成文字返回 而微信小程序上传的文件格式是silk的,而讯飞接口能识别wav 格式的文件,所以需要将小程序上传的silk文件转成wav ...
- 微信小程序语音与讯飞语音识别接口(Java),Kronopath/SILKCodec,ffmpeg处理silk,pcm,wav转换
项目需求,需要使用讯飞的语音识别接口,将微信小程序上传的录音文件识别成文字返回 首先去讯飞开放平台中申请开通语音识别功能 在这里面下载sdk,然后解压,注意appid与sdk是关联的,appid在初始 ...
随机推荐
- Python多线程练习(threading)
这几天学习python多线程的时候,试了几次thread模块和threading模块,发现thread模块非常的不好用.强烈不建议大家使用thread,建议使用threading模块,此模块对thre ...
- I/P/B/SI/SP帧和PTS/DTS的关系
I frame:帧内编码帧 又称intra picture,I 帧通常是每个 GOP(MPEG 所使用的一种视频压缩技术)的第一个帧,经过适度地压缩,做为随机访问的参考点,可以当成图象.I帧可以看成是 ...
- 入坑IT十年(二)技术以外
上一篇博客里提到:技术越来越简单,发布后不久,就看到<技术并不是越来越简单>,这显然是打擂台来了. 技术究竟是不是越来越简单?其实这个问题,要看你究竟是以什么角度来思考这个问题.我们可以举 ...
- apache一个ip多个端口虚拟主机
1.打开httpd.conf,查找Listen:80,在下面一行加入Listen:8080:2.查找#Include conf/extra/httpd-vhosts.conf,将此行前面的#去掉:3. ...
- Python自学笔记-面向对象相关(Mr seven)
---恢复内容开始--- http://www.cnblogs.com/wupeiqi/articles/5433893.html 类的成员可以分为三大类:字段.方法和属性. 一.字段 字段包括:普通 ...
- ch1-vuejs基础入门(hw v-bind v-if v-for v-on v-model 应用组件简介 小案例)
1 hello world 引入vue.min.js 代码: ----2.0+版本 <div id="test"> {{str}} </div> <s ...
- 组件 layui 表单抓取数据四步走
注意事项: layui 中提交按钮是基于"监听"机制实现的. form.on() 的调用需置于 layui.use 的回调函数中. 末尾的 'return false' 不可或缺, ...
- Mybatis Sql片段的应用
在一个查询里,针对各种不同数据库,有时候只是一部分 SQL 是不相同的,为避免相同的部分复制多次,所以将不相同的部分进行适当的隔离,再重用就可以了. 在 MyBatis 里声明两段 <sql d ...
- App 监控、推广
一.监控 酷传 http://www.kuchuan.com/ 应用雷达 http://www.ann9.com/ AppAnnie https://www.appannie.com/cn/ ap ...
- 起名字好难啊!(初识Django)
这次我们将实现一个简单的登录注册功能,并吧相应的数据写入数据库: 做这件事之前我已经在数据库中新建了两张表(当然一张表也可以用,先注册后登录嘛···) 两张结构很简单的数据表:↓ 接下来就该干正 ...