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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
| 'use strict';
const preferredDisplaySurface = document.getElementById('displaySurface');
const replayerSurface = document.querySelector('video#replayer')
const startButton = document.getElementById('startButton'); const recordButton = document.getElementById('recordButton'); const replayerButton = document.getElementById('replayer'); const downloadButton = document.getElementById('downloadButton');
let mediaRecorder; let isRecording = false; let recordedBlobs = [];
replayerSurface.ariaHidden=true
if (adapter.browserDetails.browser === 'chrome' && adapter.browserDetails.version >= 107) { document.getElementById('options').style.display = 'block'; } else if (adapter.browserDetails.browser === 'firefox') { adapter.browserShim.shimGetDisplayMedia(window, 'screen'); }
if ((navigator.mediaDevices && 'getDisplayMedia' in navigator.mediaDevices)) { startButton.disabled = false; } else { errorMsg('getDisplayMedia is not supported'); }
function handleSuccess(stream) { startButton.disabled = true; preferredDisplaySurface.disabled = true; const video = document.querySelector('video'); video.srcObject = stream; window.stream = stream; stream.getVideoTracks()[0].addEventListener('ended', () => { errorMsg('用户停止了分享屏幕'); startButton.disabled = false; preferredDisplaySurface.disabled = false; recordButton.disabled = true; }); recordButton.disabled = false; }
function handleError(error) { errorMsg(`getDisplayMedia error: ${error.name}`, error); }
function errorMsg(msg, error) { const errorElement = document.querySelector('#errorMsg'); errorElement.innerHTML += `<p>${msg}</p>`; if (typeof error !== 'undefined') { console.error(error); } }
startButton.addEventListener('click', () => { const options = {audio: true,video: { width: 1920 , height: 1080, frameRate: 15 }}; const displaySurface = preferredDisplaySurface.options[preferredDisplaySurface.selectedIndex].value;
if (displaySurface !== 'default') { options.video = {displaySurface}; } navigator.mediaDevices.getDisplayMedia(options) .then(handleSuccess, handleError); });
function getSupportedMimeTypes() { const possibleTypes = [ 'video/mp4;codecs=h264,aac', 'video/webm;codecs=vp9,opus', 'video/webm;codecs=vp8,opus', 'video/webm;codecs=h264,opus', ]; return possibleTypes.filter(mimeType => { return MediaRecorder.isTypeSupported(mimeType); }); }
function startRecording() { recordedBlobs = [];
try { mediaRecorder = new MediaRecorder(window.stream); } catch (e) { errorMsg(`创建MediaRecorder出错: ${JSON.stringify(e)}`); return; }
recordButton.textContent = '停止录制'; isRecording = true; downloadButton.disabled = true; mediaRecorder.onstop = (event) => { errorMsg('录制停止了: ' + event); }; mediaRecorder.ondataavailable = handleDataAvailable; mediaRecorder.start(); errorMsg('录制开始 mediaRecorder: ' + mediaRecorder); }
function handleDataAvailable(event) { console.log('handleDataAvailable', event); if (event.data && event.data.size > 0) { recordedBlobs.push(event.data); } }
function stopRecording() { isRecording = false; mediaRecorder.stop(); downloadButton.disabled = false; recordButton.textContent = "开始录制"; }
recordButton.addEventListener('click', () => { if (isRecording == false) { startRecording(); replayerButton.disabled = true } else { stopRecording(); replayerButton.disabled = false } });
downloadButton.addEventListener('click', () => { const blob = new Blob(recordedBlobs, { type: 'video/mp4' }); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.style.display = 'none'; a.href = url; a.download = '录屏_' + new Date().getTime() + '.mp4'; document.body.appendChild(a); a.click(); setTimeout(() => { document.body.removeChild(a); window.URL.revokeObjectURL(url); }, 100); });
replayerButton.onclick = () => { replayerSurface.disabled = true;
var blob = new Blob(recordedBlobs, { type: 'video/webm' }) replayerSurface.src = window.URL.createObjectURL(blob) replayerSurface.srcObject = null replayerSurface.controls = true replayerSurface.play() }
|