- 在 index.html中引入cdn 中的script代码:
<script src="https://cdn.jsdelivr.net/gh/xiangyuecn/Recorder@latest/src/recorder-core.js"></script>
<script src="https://cdn.jsdelivr.net/gh/xiangyuecn/Recorder@latest/src/engine/mp3.js"></script>
<script src="https://cdn.jsdelivr.net/gh/xiangyuecn/Recorder@latest/src/engine/mp3-engine.js"></script>
<script
src="https://cdn.jsdelivr.net/gh/xiangyuecn/Recorder@latest/src/extensions/frequency.histogram.view.js"></script>
<script src="https://cdn.jsdelivr.net/gh/xiangyuecn/Recorder@latest/src/extensions/lib.fft.js"></script>
- webpack.base.config.js中写入,用于在页面打包时不打包recorder包:
externals: {
'recorder': 'Recorder'
},
- 在需要用到的页面中调用
var newRec = Recorder({
type: "mp3",
sampleRate: 16000,
bitRate: 16,
onProcess(buffers, powerLevel, bufferDuration, bufferSampleRate) {
document.querySelector(".duration").innerText =
formatDuring(bufferDuration);
wave.input(buffers[buffers.length - 1], powerLevel, bufferSampleRate);
},
success:function(dd){
console.log('dd', dd)
},error:function(msg){
console.log('cc', msg)
}
});
this.createDelayDialog();
newRec.open(
() => {
this.dialogCancel();
this.rec = newRec;
wave = Recorder.FrequencyHistogramView({ elem: ".recwave" });
this.beginAnswerDisabled = false;
this.startRecord();
},
(msg, isUserNotAllow) => {
this.dialogCancel();
if (isUserNotAllow) {
this.$confirm(
"您拒绝了该问卷获取录音权限,您将不能答题,是否重新获取?",
"提示",
{
type: "warning",
}
).then(({ result }) => {
if (result) {
this.recOpen();
} else {
this.$toast.error(msg + ",请刷新页面重新获取");
}
});
} else {
this.$toast.error(msg + ",您将不能答题");
}
}
);
window.waitDialogClick = () => {
this.dialogCancel();
this.$toast.error("麦克风权限请求被忽略,您将不能答题");
};
- createDelayDialog文件:
createDelayDialog() {
this.dialogInt = setTimeout(() => {
this.showDialog();
}, 8000);
}