<div> <span onclick="start()">开始录音</span> <span onclick="stop()">结束录音</span> </div> <script> const constraints = { audio: true }; var mediaRecorder = null; function start ( ) { var chunks = []; console.log("start") navigator.mediaDevices.getUserMedia(constraints) .then(stream => { mediaRecorder = new MediaRecorder(stream); mediaRecorder.addEventListener('dataavailable', event => { chunks.push(event.data); }); mediaRecorder.addEventListener('stop', () => { const blob = new Blob(chunks, { type: 'audio/wav' }); const url = URL.createObjectURL(blob); console.log(blob, url) const link = document.createElement('a'); link.href = url; link.download = 'recording.wav'; document.body.appendChild(link); link.click(); }); mediaRecorder.start(); }) .catch(err => { console.error(err); }); } function stop ( ) { mediaRecorder.stop(); console.log("stop" ,mediaRecorder) } </script>
标签:mediaRecorder,console,stop,录音,js,link,本地,chunks,const From: https://www.cnblogs.com/guozhongbo/p/17516259.html