小程序 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在初始 ...
随机推荐
- CSS中新属性calc()
CSS3的calc()使用 原文: http://www.w3cplus.com/css3/how-to-use-css3-calc-function.html © w3cplus.com calc( ...
- Python自学笔记-面向对象相关(Mr seven)
---恢复内容开始--- http://www.cnblogs.com/wupeiqi/articles/5433893.html 类的成员可以分为三大类:字段.方法和属性. 一.字段 字段包括:普通 ...
- myeclipse 中clean的作用
myeclipse中clean的作用 重新编译的功能 就是将编译好的class文件都删除后在重新生成.如果引用的jar包不能工作可以尝试下.
- sqlserver 使用脚本创建Sql Server代理作业
use master GO /* --开启sql server代理 sp_configure 'show advanced options', 1; GO RECONFIGURE; GO sp_con ...
- 初学者易上手的SSH-struts2 02Action获取表单数据-通配符
在上一章中,我们已经搭建好了struts2的一个开发环境,那么这一章就来做一个简单的登录功能,以及介绍和使用struts2里面一个重要的东西-通配符. 第一步,在WebContent下面新建一个log ...
- iOS自动化环境搭建——macaca
macaca-java for ios 自动化环境搭建 基础原理解析:https://testerhome.com/topics/6608 一.环境搭建 1.安装eclipse; -----Java开 ...
- JS实现时钟特效
<!DOCTYPE html><html><head lang="en"><meta charset="UTF-8"& ...
- 什么是UUID?
1.定义 UUID含义是通用唯一识别码 (Universally Unique Identifier),这 是一个软件建构的标准,也是被开源软件基金会 (Open Software Foundatio ...
- TQ2440--nandflash(K9F2G08U0A)驱动编写
一.数据手册相关内容 1.地址传输周期 2.命令表 3.在寄存器中,会涉及TACLS,TWRPH0,TWRPH1的设定 这里我们就去看nandflash的数据手册 在这里我们可以清楚的看到,TACLS ...
- session和cookie作用原理,区别
Cookie概念 在浏览某些 网站 时,这些网站会把 一些数据存在 客户端 , 用于使用网站 等跟踪用户,实现用户自定义 功能. 是否设置过期时间: 如果不设置 过期时间,则表示这个 Cookie生命 ...