前言
如题:做一个录音文字识别功能,知识点有三个,分别是微信小程序的录音功能、录音文件直传阿里云OSS、使用阿里云的录音文件识别接口返回识别后的文字
一、微信小程序录音
官方文档:微信小程序全局唯一的录音管理器 RecorderManager
wxml:
<view style="padding-top:40%"></view>
<button bindtap="start">开始录音</button>
<!-- <button bindtap="pause">暂停录音</button> -->
<!-- <button bindtap="resume">继续录音</button> -->
<view style="padding-top:10px"></view>
<button bindtap="stop">停止录音并识别</button>
<!-- <button bindtap="play">播放录音[本地]</button> -->
<!-- <button bindtap="play">播放录音[线上]</button> -->
<view style="text-align:center;padding-top:20px">{{text}}</view>
js:需要注意的是,点击开始录音时要判断当前是否获取到了录音权限,如果没有录音权限进行提示,引导用户重新授权
const recorderManager = wx.getRecorderManager()
const innerAudioContext = wx.createInnerAudioContext()
Page({
data: {
text: '语音识别后文字展示'
},
onLoad: function () {
},
//开始录音的时候
start: function () {
var that = this
const options = {
duration: 100000, //指定录音的时长,单位 ms
sampleRate: 16000, //采样率
numberOfChannels: 1, //录音通道数
encodeBitRate: 96000, //编码码率
format: 'mp3', //音频格式,有效值 aac/mp3
frameSize: 50, //指定帧大小,单位 KB
}
//开始录音
wx.authorize({
scope: 'scope.record',
success() {
console.log("录音授权成功");
//第一次成功授权后 状态切换为2
that.setData({
status: 2,
})
recorderManager.start(options);
recorderManager.onStart(() => {
//可以弹出模态框等友好提示
console.log('录音开始')
});
//错误回调
recorderManager.onError((res) => {
//进行错误提示
console.log(res);
})
},
fail() {
console.log("第一次录音授权失败");
wx.showModal({
title: '提示',
content: '您未授权录音,功能将无法使用',
showCancel: true,
confirmText: "授权",
confirmColor: "#52a2d8",
success: function (res) {
if (res.confirm) {
//确认则打开设置页面(重点)
wx.openSetting({
success: (res) => {
console.log(res.authSetting);
if (!res.authSetting['scope.record']) {
//未设置录音授权
console.log("未设置录音授权");
wx.showModal({
title: '提示',
content: '您未授权录音,功能将无法使用',
showCancel: false
})
} else {
//第二次才成功授权
console.log("设置录音授权成功");
that.setData({
status: 2
})
}
},
fail: function () {
console.log("授权设置录音失败");
}
})
} else if (res.cancel) {
console.log("cancel");
}
},
fail: function () {
console.log("openfail");
}
})
}
})
},
//暂停录音-需求不需要暂停
pause: function () {
recorderManager.pause();
recorderManager.onPause((res) => {
console.log('暂停录音')
})
},
//继续录音-不需要暂停也就不需要继续
resume: function () {
recorderManager.resume();
recorderManager.onStart(() => {
console.log('重新开始录音')
});
//错误回调
recorderManager.onError((res) => {
console.log(res);
})
},
//停止录音
stop: function () {
recorderManager.stop();
recorderManager.onStop((res) => {
console.log('停止录音', res.tempFilePath)
this.getPolicy(res.tempFilePath)
})
},
//播放声音
play: function () {
innerAudioContext.autoplay = true
innerAudioContext.src = this.tempFilePath,
innerAudioContext.onPlay(() => {
console.log('开始播放')
})
innerAudioContext.onError((res) => {
console.log(res.errMsg)
console.log(res.errCode)
})
}
})
二、微信小程序录音直传阿里云OSS
官方文档:微信小程序直传实践
如文档所示,首先进行配置Bucket跨域访问,这个不做解释,其次在微信小程序中配置域名白名单也就是uploadFile和downloadFile合法域名为对应Bucket的外网访问域名
Javascript直传OSS需要提前获取policy,服务端如下:
public Map<String, String> ossPolicy() {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
String path = "data/" + dateFormat.format(date) + "/";
try {
long expireTime = 3000;
long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
Date expiration = new Date(expireEndTime);
PolicyConditions policyConds = new PolicyConditions();
policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, path);
String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
byte[] binaryData = postPolicy.getBytes("utf-8");
String encodedPolicy = BinaryUtil.toBase64String(binaryData);
String postSignature = ossClient.calculatePostSignature(postPolicy);
Map<String, String> respMap = new LinkedHashMap<String, String>();
respMap.put("accessid", ossClient.getCredentialsProvider().getCredentials().getAccessKeyId());
respMap.put("policy", encodedPolicy);
respMap.put("signature", postSignature);
respMap.put("dir", path);
respMap.put("host", configService.findConfigValueBySuffix(ConfigConstant.CONFIG_SUFFIX_RESOURCE_URL));
respMap.put("expire", String.valueOf(expireEndTime / 1000));
return respMap;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
获取到policy以及签名等后,直传录音文件到阿里云OSS:
getPolicy: function (path) {
wx.request({
url: 'https://xxx.com/oss/policy',
method: 'GET',
success: (res) => {
console.log('获取policy成功' + path)
this.upload(res, path)
}
})
},
upload: function (res, path) {
const host = res.data.datas.host;
const signature = res.data.datas.signature;
const ossAccessKeyId = res.data.datas.accessid;
const policy = res.data.datas.policy;
//这里的key实际是新起的文件名和路径
const key = res.data.datas.dir + Date.now() + '.mp3';
wx.uploadFile({
url: host,
filePath: path,
name: 'file', // 必须填file。
formData: {
key,
policy,
OSSAccessKeyId: ossAccessKeyId,
signature,
},
success: (res) => {
console.log(res);
if (res.statusCode === 204) {
console.log('上传成功');
this.toText(key)
}
},
fail: err => {
console.log(err);
}
});
}
三、阿里云录音文字识别极速版
进行录音识别之前需要先创建智能语音交互服务对应的项目,获得AppKey
官方文档:创建智能语音交互服务项目
官方文档:录音文件识别极速版文章来源:https://www.toymoban.com/news/detail-496624.html
由于将录音文件直传OSS了,所以不需要将录音文件上传到语音识别里,所以采取第二种方式,使用音频文件链接进行语音识别:文章来源地址https://www.toymoban.com/news/detail-496624.html
public ResultBody record2text(String recordUrl) throws Exception {
Assert.isBlank(recordUrl, JobErrorInfoEnum.PARAM_NOT_NULL);
String prefix = configService.findConfigValueBySuffix(ConfigConstant.CONFIG_SUFFIX_RESOURCE_URL);
AccessToken accessToken = new AccessToken(id, secret);
accessToken.apply();
String token = accessToken.getToken();
String fileName = prefix + recordUrl;
String format = "mp3";
int sampleRate = 16000;
String allText = process(fileName, appKey, token, format, sampleRate);
Assert.isBlank(allText, JobErrorInfoEnum.RECORD_TRANS_ERROR);
return ResultBody.success(allText);
}
public String process(String fileName, String appKey, String token, String format, int sampleRate) {
/**
* 设置HTTPS REST POST请求
* 1.使用http协议
* 2.语音识别服务域名:nls-gateway.cn-shanghai.aliyuncs.com
* 3.语音识别接口请求路径:/stream/v1/FlashRecognizer
* 4.设置必须请求参数:appkey、token、format、sample_rate
*/
String request = url;
request = request + "?appkey=" + appKey;
request = request + "&token=" + token;
request = request + "&format=" + format;
request = request + "&sample_rate=" + sampleRate;
/**
* 设置HTTPS头部字段
* 发送HTTPS POST请求,返回服务端的响应。
*
* 1.Content-Type:application/octet-stream
*/
HashMap<String, String> headers = new HashMap<String, String>();
String response;
if (new File(fileName).isFile()) {
headers.put("Content-Type", "application/octet-stream");
response = HttpUtil.sendPostFile(request, headers, fileName);
} else {
headers.put("Content-Type", "application/text");
response = HttpUtil.sendPostLink(request, headers, fileName);
}
String allText = "";
if (response != null) {
Map map = JsonUtils.json2Map(response);
if ((int) map.get("status") == 20000000) {
Map result = (Map) map.get("flash_result");
List<Map> sentences = (List<Map>) result.get("sentences");
if (sentences != null && sentences.size() > 0) {
for (Map sentence : sentences) {
allText += sentence.get("text");
}
}
}
}
return allText;
}
到了这里,关于微信小程序录音直传阿里云OSS并语音识别的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!