小程序 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在初始 ...
随机推荐
- Day2 python基础学习
http://www.pythondoc.com/ Python中文学习大本营 本节内容: 一.字符串操作 二.列表操作 三.元组操作 四.字典操作 五.集合操作 六.字符编码操作 一.字符串操作 1 ...
- vue数组语法兼容问题
先来一行代码: <a :href="['NewsNote.asp?ID='+item.ID+'&MenuType=C']" v-text="item.Tit ...
- python修改注册表
与注册表操作相关的函数可以分为打开注册表.关闭注册表.读取项值.c添加项值.添加项,以及删除项等几类. 表1 Windows注册表基本项 项名 描述 HKEY_CLASSES_ROOT 是HKEY ...
- Java web 入门知识 及HTTP协议详解
Java web 入门知识 及HTTP协议详解 WEB入门 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资 ...
- Java高新技术 Myeclipse 介绍
Java高新技术 Myeclipse 介绍 知识概述: (1)Myeclipse开发工具介绍 (2)Myeclipse常用开发步骤详解 ...
- C#一款比较美观的验证码
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System. ...
- 学习总结---OVS
OVS的组成 OVS的匹配条件和动作 OVS的发展方向 OVS的实践 OVS与Namespace配合模拟租户之间的数据通信 基本思路: Namespace模拟出不同的主机,这些主机之间的通信需要通过S ...
- 即时通信系统Openfire分析之八:集群管理
前言 在第六章<路由表>中,客户端进行会话时,首先要获取对方的Session实例.获取Session实例的方法,是先查找本地路由表,若找不到,则通过路由表中的缓存数据,由定位器获取. 路由 ...
- WPF checkbox文字下掉
WPF checkbox文字下掉 可以使用 <Style TargetType="CheckBox"> <Setter Property="Margin ...
- VisualStudio 合并代码文件
如果有相同的类,一般可以使用 partial 让他写在多个文件,那么如何把多个文件合并?请看 MainWindow.xaml 和 MainWindow.xaml.cs 其中 代码文件被折叠,那么如何做 ...