Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript MediaRecorder录制块_Javascript_Html5 Audio - Fatal编程技术网

使用javascript MediaRecorder录制块

使用javascript MediaRecorder录制块,javascript,html5-audio,Javascript,Html5 Audio,下面的代码允许用户启动录制,然后将其播放给用户: <body> <div> <h2>Audio record and playback</h2> <p> <button id=startRecord>start</button> <button id=stopRecord disabled>stop</button&

下面的代码允许用户启动录制,然后将其播放给用户:

<body>
<div>
        <h2>Audio record and playback</h2>
        <p>
            <button id=startRecord>start</button>
            <button id=stopRecord disabled>stop</button>
        </p>    
        <p>
            <audio id=recordedAudio></audio>
            <a id=audioDownload></a>
        </p>
</div>
</body>


<script>


(function() {
   // your page initialization code here
   // the DOM will be available here


navigator.mediaDevices.getUserMedia({audio:true})
    .then(stream => {
        rec = new MediaRecorder(stream);
        rec.ondataavailable = e => {
            audioChunks.push(e.data);
            if (rec.state == "inactive"){
        let blob = new Blob(audioChunks,{type:'audio/x-mpeg-3'});
        recordedAudio.src = URL.createObjectURL(blob);
        recordedAudio.controls=true;
        recordedAudio.autoplay=true;
        audioDownload.href = recordedAudio.src;
        audioDownload.download = 'mp3';
        audioDownload.innerHTML = 'download';
     }
        }
    })
    .catch(e=>console.log(e));

startRecord.onclick = e => {
  startRecord.disabled = true;
  stopRecord.disabled=false;
  audioChunks = [];
  rec.start();
}
stopRecord.onclick = e => {
  startRecord.disabled = false;
  stopRecord.disabled=true;
  rec.stop();
}



})();

</script>

rec.start();
var i = 0, howManyTimes = 10;
function f() {
    i++;
    if( i < howManyTimes ){
        setTimeout( f, 5000 );
        rec.stop();
        rec.start();
    }

}
f();
ReferenceError: rec is not defined