本文共 1436 字,大约阅读时间需要 4 分钟。
按住说话,松开结束
播放录音
录音中{
{intIntervalTime}}s>>>const recorderManager = uni.getRecorderManager();
const innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.autoplay = true;
export default {
data() {
return {
text: "uni-app",
voicePath: "",
isRecord: false, // 记录状态,录音中或者是未开始
intervalTime: 0,
timer: null
};
},
onLoad() {
let self = this;
recorderManager.onStop(function(res) {
console.log("录音停止了" + JSON.stringify(res)); //返回录音的临时保存地址, 可用于后面的播放
self.voicePath = res.tempFilePath;
});
},
computed: {
intIntervalTime() {
// 用于显示整数的秒数
console.log(Math.round(this.intervalTime));
return Math.round(this.intervalTime);
}
},
methods: {
startRecord() {
this.timer = setInterval(() => {
this.intervalTime += 0.5;
if (this.intervalTime >= 0.5 && !this.isRecord) {
//如果用户录制的时间太短,就不会去开启录音, 因为有个bug: recorderManager.stop()在短时间内开启在关闭的话,实际上他还在不停地录音,不知道你们有没有遇到过
console.log("开始录音");
this.isRecord = true;
this.intervalTime = 0;
recorderManager.start({
format: "mp3"
});
}
}, 500);
},
endRecord() {
if (this.intervalTime <= 0.5) {
console.log("录音太短了!!!");
}
clearInterval(this.timer);
if (this.isRecord) {
setTimeout(() => {
recorderManager.stop();
this.isRecord = false;
console.log(this.isRecord);
}, 500); //延迟小段时间停止录音, 更好的体验
}
},
playVoice() {
console.log("播放录音");
if (this.voicePath) {
innerAudioContext.src = this.voicePath;
innerAudioContext.play();
}
}
}
};
.record-container {
padding-top: 100upx;
}
.status {
text-align: center;
}
转载地址:https://blog.csdn.net/weixin_33682804/article/details/114008273 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!