1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| console.log(navigator.mediaDevices); var mediaRecorder navigator.mediaDevices.getUserMedia({ audio: true }) .then(function(stream) { mediaRecorder = new MediaRecorder(stream);
var chunks = [];
mediaRecorder.ondataavailable = function(event) { if (event.data.size > 0) { chunks.push(event.data); console.log(chunks); } }; mediaRecorder.onstop = function() { var audioBlob = new Blob(chunks, { type: 'audio/wav' }); const link = document.createElement('a') link.style.display = 'none' link.href = URL.createObjectURL(audioBlob) link.download = `a${Math.floor(Math.random()*9999)}.mp3` document.body.appendChild(link) link.click() document.body.removeChild(link) };
}) .catch(function(err) { console.error('获取麦克风失败:', err); }); document.querySelector("#start").addEventListener('click',function(){ console.log('开始'); mediaRecorder.start(); },false) document.querySelector("#end").addEventListener('click',function(){ console.log('结束'); mediaRecorder.stop(); },false)
|