这是WXML文件
recordingTimer:function(){
var that = this;
that.data.setInter = setInterval(
function(){
var time = that.data.recordingTimerqwe+1;
if(time>60){
wx.showToast({
title: 'The recording time is up to one minute',
duration:1500,
mask:true
})
clearInterval(that.data.setInter);
that.shutRecording();
return;
}
that.setData({
recordingTimerqwe:time
})
}
,1000);
},
//为录音文件生成随机数
randomNum(minNum, maxNum) {
switch (arguments.length) {
case 1:
return parseInt(Math.random() * minNum + 1, 10);
break;
case 2:
return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
break;
default:
return 0;
break;
}
},
openRecording:function(){
var that = this;
// wx.getSystemInfo({
// success:function(res){
// }
// })
recordManager.onStart(()=>{
console.log('[监听]....开始录音.....')
});
recordManager.onFrameRecorded((res) => {
const { frameBuffer } = res
console.log('frameBuffer.byteLength', frameBuffer.byteLength)
})
const options = {
duration: 60000, //录音的时长,之前最大值好像只有1分钟,现在最长可以录音10分钟
format: 'mp3', //录音的格式,有aac和mp3两种 // }
sampleRate:16000,
numberOfChannels:1,
encodeBitRate:96000,
frameSize:50,
}
//that.recordingTimer();
recordManager.start(options);
console.log('开始录音')
// recordManager.onError((res)=>{
// console.log(res);
// })
},
ontouchstart: function (e) {
wx.vibrateLong();//震动
wx.showToast({//按后效果
title:'按住录音',
duration:1000,
mask:true,
})
// 传到智聆,评测音频各参数,开始录音
this.openRecording();
app.globalData.manager.start({
content:this.data.content,
evalMode: this.data.evalMode,//选择智聆评测模式,evalMode: 0,//评测模式,1:句子模式 0:词模式
scoreCoeff: this.data.scoreCoeff,
serverType: this.data.serverType,
storageMode: this.data.storageMode,
soeAppId: this.data.soeAppId,
textMode: this.data.textMode,
duration: this.data.duration
});
this.setData({
view: 1,
});
// console.log('开始录音,view为',this.data.view);
},
shutRecording: function(){
// async shutRecording(){
var that = this;
// wx.getSystemInfo({
// })
recordManager.stop();
console.log('停止录音')
recordManager.onStop((res)=>{
console.log('[监听]..停止录音...',res.tempFilePath)
const{tempFilePath} =res;
clearInterval(that.data.setInter);
console.log(res)
let time = parseInt(res.duration/1000);
this.setData({
voice1:res,
voiceTime:time,
})
//上传临时录音文件进云存储
wx.cloud.uploadFile({
cloudPath:"studentvoice/"+app.globalData.username+"/"+that.randomNum(10000,99999)+'.mp3',
filePath:res.tempFilePath,
success: res => {
console.log('[上传录音] 成功: ', res)
this.setData({
voiceID: res._id,
voice: res,
});
let url = ""
console.log('[fileID]: ', res.fileID)
wx.cloud.getTempFileURL({
fileList: [res.fileID],
success: res => {
//console.log(fileID)
url = res.fileList[0].tempFileURL
this.setData({
voiceurl: url,
});
// this.onLoad();
console.log('[录音路径获取] 成功: ', url)
console.log('[用户ID获取] 成功: ', app.globalData.userid)
console.log('[用户姓名获取] 成功: ', app.globalData.name)
console.log('[用户学号获取] 成功: ', app.globalData.username)
console.log('[周数获取] 成功: ', app.globalData.weekid)
console.log('[句子id获取]成功',this.data.passageid)
console.log('[句子分数获取]成功',this.data.SuggestedScore)//该函数与测评函数不在同一进程,调试器测评函数运行更慢,所以获取不到数据,但是直接打开小程序可以正常使用
//抓取现在的时间
function formatDateTime(time) {
var date = new Date(time);
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
var d = date.getDate();
d = d < 10 ? ('0' + d) : d;
var h = date.getHours();
h = h < 10 ? ('0' + h) : h;
var minute = date.getMinutes();
var second = date.getSeconds();
minute = minute < 10 ? ('0' + minute) : minute;
second = second < 10 ? ('0' + second) : second;
return y + '/' + m + '/' + d+' '+h+':'+minute+':'+second;
};
let nowtime = formatDateTime(new Date())
console.log(nowtime)
wx.cloud.database().collection("voice_data").add({
data: {
//voiceID: res.fileID,
//voice: res,
url: url,
downloadurl: url,
name: app.globalData.name,
username: app.globalData.username,
weekid: app.globalData.weekid,
passageid: this.data.passageid,
suggestedScore: this.data.SuggestedScore,//写一个选择判断,决定存进去的分是多少
pronAccuracyScore: this.data.PronAccuracy,
pronFluencyScore: this.data.PronFluency,
pronCompletionScore: this.data.PronCompletion,
maximumScore: this.data.maximumScore,
settime: nowtime,
},
success: function () {
console.log('[录音上传到数据库] 成功')
},
fail: function(){
console.log('[录音上传到数据库] 失败')
}
})
},
fail: res => {
console.log('[录音路径获取] 失败')
}
})
}
})
})
},
//测评结束
ontouchend:function(e){
console.log('结束')
let that = this;
that.shutRecording();
app.globalData.manager.stop();
that.setData({
practicetime:that.data.practicetime+1,
})
this.setData({
view: 0,
});
},
这是录音部分的主要代码,用的是getRecorderManager.
调试视频如下:
(手机端)
(电脑端)
调试后发现,原因是调用了智聆的接口,整个过程比较慢,所以导致了这种情况,但是智聆是要调用的,请问大家我要如何解决这种问题?
我不是很熟悉小程序,但是 onStop 是不是 不应该在 stop 之后调用
demo测试没有任何问题,但是实际运用中会加入一些其他的功能,所以不知道我这些代码的问题出在哪,下面是demo: // pages/record/record.js // 两个实例声明在Page之外,方便访问 const recorderManager = wx.getRecorderManager() //这是录音功能的实例,必须的 const innerAudioContext = wx.createInnerAudioContext(); //这是播放录音功能需要的实例 Page({ data: { tempFilePath: '' //存放录音文件的临时路径 }, // 播放录音 playVoice: function(e) { innerAudioContext.onPlay(() => { console.log('开始播放') }) innerAudioContext.onError((res) => { console.log(res.errMsg) console.log(res.errCode) }) innerAudioContext.play(); }, // 录音 beginRecord:function(e) { wx.showToast({//按后效果 title:'按住录音', duration:1000, mask:true, }) // 监听录音开始事件 recorderManager.onStart(() => { console.log('recorder onstart') }) // 监听已录制完指定帧大小的文件事件。如果设置了 frameSize,则会回调此事件。 recorderManager.onFrameRecorded((res) => { const { frameBuffer } = res console.log('frameBuffer.byteLength', frameBuffer.byteLength) }) //录音的参数 const options = { duration: 60000, //录音时间,默认是60s,提前松手会触发button的bindtouchend事件,执行停止函数并上传录音文件。超过60s不松手会如何并未测试过 sampleRate: 44100, numberOfChannels: 1, encodeBitRate: 192000, format: 'mp3', //录音格式,这里是mp3 frameSize: 50 //指定帧大小,单位 KB。传入 frameSize 后,每录制指定帧大小的内容后,会回调录制的文件内容,不指定则不会回调。暂仅支持 mp3 格式。 } //开始录音 recorderManager.start(options); console.log('recorder start') }, //停止录音并上传数据 endRecord:function(e) { const self = this; //停止录音 recorderManager.stop(); console.log('停止Stop') //监听录音停止事件,执行上传录音文件函数 recorderManager.onStop((res) => { console.log('recorder stop', res) console.log('停止Onstop') //返回值res.tempFilePath是录音文件的临时路径 (本地路径) self.setData({ tempFilePath: res.tempFilePath }) innerAudioContext.src = res.tempFilePath }) } }) 这个demo双端都没有问题