js10行代码实现屏幕录制

var body = document.body;
body.addEventListener("click", async function () {
    var stream = await navigator.mediaDevices.getDisplayMedia({ video: true });
    var mime = MediaRecorder.isTypeSupported("video/webm; codecs=vp9") ? "video/webm; codecs=vp9" : "video/webm";
    var mediaRecorder = new MediaRecorder(stream, { mimeType: mime });
    var chunks = [];
    mediaRecorder.addEventListener('dataavailable', function (e) { chunks.push(e.data) })
    mediaRecorder.addEventListener('stop', function () {
        var blob = new Blob(chunks, { type: chunks[0].type });
        var url = URL.createObjectURL(blob); var a = document.createElement('a');
        a.href = url; a.download = 'video.webm'; a.click();
    })        //手动启动     
    mediaRecorder.start()
});

  

posted @ 2022-11-22 15:09  银河游鱼  阅读(64)  评论(0编辑  收藏  举报