JS视频和音频录制

来源:互联网 发布:哒哒abc少儿英语 知乎 编辑:程序博客网 时间:2024/06/05 16:40
<body>    <video autoplay></video>    <a download="media" class="downloadButton">下载</a>    <button class="recorderControl">录制</button></body><script>    var promise= navigator.mediaDevices.getUserMedia({video:true});    promise.then(function(stream){        var video=document.querySelector("video");        video.src = URL.createObjectURL(stream);        var recorder=new MediaRecorder(stream);        var recorderControl=document.querySelector(".recorderControl");         recorderControl.onclick=function(){            this.textContent=="录制"?video.play():video.pause();            this.textContent=="录制"?recorder.start():recorder.stop();            this.textContent=this.textContent=="录制"?"停止":"录制";            };        var buffers=[];        recorder.ondataavailable=function(event){            buffers=event.data;        }        var downloadButton=document.querySelector(".downloadButton");        recorder.onstop=function(){            var url=URL.createObjectURL(buffers);            downloadButton.click();            buffers=null;        };     }).catch(function(error){        console.log(error);     });    // 音频同理,只需改变标签即可。</script>
原创粉丝点击