//index.js
//获取应用实例
const app = getApp()
//引入插件:微信同声传译
const plugin = requirePlugin('WechatSI');
//获取全局唯一的语音识别管理器recordRecoManager
const manager = plugin.getRecordRecognitionManager();
Page({
data: {
src: '', //路径
recordState: false, //录音状态
content:'',//识别的内容
},
onLoad: function (options) {
console.log('load');
//识别语音
this.initRecord();
},
onShow(){
// 获取录音授权
this.getRecordAuth()
},
// 权限询问
getRecordAuth: function() {
wx.getSetting({
success: (res) => {
if (!res.authSetting['scope.record']) {
wx.authorize({
scope: 'scope.record',
success() {
// 用户已经同意小程序使用录音功能,后续调用 wx.startRecord 接口不会弹窗询问
console.log("succ auth")
}, fail: () => {
console.log("fail auth")
this.userAuthFail('scope.record', '请授权录音服务,用于获取语音识别').then(authRecordRes => {
console.log(authRecordRes);
}).catch(authRecordErr => {
console.log(authRecordErr);
wx.showToast({
title: authRecordErr,
icon: 'none',
duration: 2000,
})
})
}
})
} else {
console.log("record has been authed")
}
}, fail(res) {
console.log("fail")
console.log(res)
}
})
},
/**
* 用户拒绝授权
* @param {string} scope 需授权的权限
* @param {string} tip 权限对应的提示
*/
userAuthFail(scope, tip) {
return new Promise((resolve, reject) => {
wx.showModal({
title: '提示',
content: tip,
confirmText: '去授权',
cancelText: '不授权',
success(res) {
if (res.confirm) {
wx.openSetting({
success: (res) => {
resolve(res.authSetting[scope])
}
})
}
if (res.cancel) {
reject('您拒绝了授权')
}
},
})
})
},
// 文字转语音
start: function (e) {
var that = this;
plugin.textToSpeech({
lang: "zh_CN",
tts: true,
content: "你好吗" ,
success: function (res) {
console.log(res);
console.log("succ tts", res.filename);
that.setData({
src: res.filename
})
// 播报语音
that.yuyinPlay();
},
fail: function (res) {
console.log("fail tts", res)
}
});
},
//播放语音
yuyinPlay: function (e) {
if (this.data.src == '') {
console.log("暂无语音");
return;
}
this.innerAudioContext.src = this.data.src //设置音频地址
this.innerAudioContext.play(); //播放音频
},
// 结束语音
end: function (e) {
this.innerAudioContext.pause(); //暂停音频
},
onReady: function () {
//创建内部 audio 上下文 InnerAudioContext 对象。
this.innerAudioContext = wx.createInnerAudioContext();
this.innerAudioContext.onError(function (res) {
console.log(res);
wx.showToast({
title: '语音播放失败',
icon: 'none',
})
})
},
//识别语音 -- 初始化
initRecord: function () {
const that = this;
// 有新的识别内容返回,则会调用此事件
manager.onRecognize = function (res) {
console.log(res)
if (res.result === '') return
const text = that.data.content + res.result
that.setData({
content: text
})
}
// 正常开始录音识别时会调用此事件
manager.onStart = function (res) {
console.log("成功开始识别", res)
}
// 识别错误事件
manager.onError = function (res) {
console.log("error");
console.error("error msg", res)
}
//识别结束事件
manager.onStop = function (res) {
console.log('------结束-------')
console.log(res);
console.log('录音临时文件地址 -->' + res.tempFilePath);
console.log('录音总时长 -->' + res.duration + 'ms');
console.log('文件大小 --> ' + res.fileSize + 'B');
console.log('语音内容 --> ' + res.result);
if (res.result == '') {
that.playTextToVoice('我没有听清楚,请重新说一遍!')
return;
}
var text = that.data.content + res.result;
that.setData({
content: text
})
}
},
//语音 --按住说话
touchStart: function (e) {
this.setData({
recordState: true //录音状态
})
// 语音开始识别
manager.start({
lang: 'zh_CN',// 识别的语言,目前支持zh_CN en_US zh_HK sichuanhua
duration:30000
})
},
//语音 --松开结束
touchEnd: function (e) {
this.setData({
recordState: false
})
// 语音结束识别
manager.stop();
},
})
有秒数限制,你好,建议30s内,超过30s,拆分多次录音