Files
2025-12-01 03:42:34 +08:00

70 lines
1.6 KiB
Vue

<template>
<view class="container">
<button @click="onStartRecord">开始录音</button>
<button @click="onStopRecord">停止录音</button>
<view class="tip">{{ status }}</view>
<yao-RecordFrame ref="recordFrame" @onFrameRecorded="frameRecorded" @currentDecibels="onCurrentDecibels"
@onStop="stopIt"></yao-RecordFrame>
</view>
</template>
<script>
export default {
data() {
return {
status: "未录音",
recorder: null, // 录音实例
recordTimer: null, // 定时读取定时器
};
},
onUnload() {
},
methods: {
// 申请录音权限
async applyRecordPermission() {
const res = await uni.requestPermissions({
scope: "scope.record"
});
return res[0].grantStatus === 1; // 1 表示授权成功
},
onStartRecord() {
//开启录音(仅支持两种参数)
this.$refs.recordFrame.start({
sampleRate: 16000,
frameSize: 1024,
gain: 1.0 //增益值,数字越高音频声音越大 1.0~20.0
})
},
onStopRecord() {
//停止录音
this.$refs.recordFrame.stop();
},
//停止录音
stopIt(base64) {
//base64音频只能在浏览器播放
//也可以base64音频转成文件音频可app播放
// console.log(base64);
},
frameRecorded({
isLastFrame,
frameBuffer
}) {
console.log(isLastFrame, frameBuffer);
if (!isLastFrame) {
this.frameBuffer = frameBuffer;
}
this.isLastFrame = isLastFrame ? 'true' : 'false';
},
onCurrentDecibels(decibels) {
//当前分贝值 最小可测分贝(-80) 最大可测分贝(-30)
console.log("当前分贝:" + decibels)
}
}
};
</script>
<style scoped>
</style>