编写了一个简单的录制音频的页面,功能是在页面加载的时候开始录音,倒计时20s之后停止录音并将录音上传到云存储。在windows开发者平台进行测试,当我从云存储下载临时的录制文件到本地的时候,录制的音频文件时长异常,音频文件大小是正常的为100k左右,以下是时长异常的截图和代码片段:
const recorderManager = wx.getRecorderManager();
Page({
data: {
countdown: 20,
},
onLoad: function () {
this.startRecording();
this.startCountdown();
},
startRecording: function () {
const options = {
format: "mp3",
sampleRate: 22050,
numberOfChannels: 1,
encodeBitRate: 124000,
frameSize: 50,
};
recorderManager.start(options);
},
startCountdown: function () {
let countdown = 20;
const countdownInterval = setInterval(() => {
countdown--;
this.setData({ countdown: countdown });
if (countdown === 0) {
clearInterval(countdownInterval);
this.stopRecording();
}
}, 1000);
},
stopRecording: function () {
recorderManager.stop();
recorderManager.onStop((res) => {
console.log('录音结束', res);
const { tempFilePath } = res;
wx.cloud.uploadFile({
cloudPath: 'recordedfile_user.mp3',
filePath: tempFilePath
})
setTimeout(function() {
wx.navigateTo({
url: `/pages/recognize/recognize`,
});
}, 3000); // 延时3秒
});
}
})