getusermedia录制在等待MDN示例后不要停止



此示例使用MDN上的承诺抓住/录制视频流以飞行的方式进行工作,直到单击停止录制按钮,然后全部停止,还可以正确停止,还可以导致音频/视频硬件在浏览器上关闭:但是,如果您在不单击停止按钮的情况下让倒数时间播放时间,您会发现该示例不会关闭将仍将打开的音频/视频,例如仍在录制(并且仍然参与了硬件结果(:https://developer.mozilla.org/en-us/docs/web/api/mediastream_recording_api/recording_a_media_element(工作示例位于同一页面的底部或链接的Codepen或JSFiddle上(当等待功能经过时间时,应该如何将其关闭,而无需单击"停止"按钮?有人知道是否可能?

这是很有可能的。这是我提出的代码,现在一切都停止了,即使没有单击按钮停止:如果单击是模拟的,以防万一。感谢这个推荐https://developer.mozilla.org/en-us/docs/web/guide/events/creating_and_triggering_events

所得(工作正常(代码就是这样(完成/粘贴为HTML文件以进行测试(:

<!DOCTYPE html>
<html>
    <head>
        <style type="text/css">
            body {
  font: 14px "Open Sans", "Arial", sans-serif;
}
video {
  margin-top: 2px;
  border: 1px solid black;
}
.button {
  cursor: pointer;
  display: block;
  width: 160px;
  border: 1px solid black;
  font-size: 16px;
  text-align: center;
  padding-top: 2px;
  padding-bottom: 4px;
  color: white;
  background-color: darkgreen;
  text-decoration: none;
}
h2 {
  margin-bottom: 4px;
}
.left {
  margin-right: 10px;
  float: left;
  width: 160px;
  padding: 0px;
}
.right {
  margin-left: 10px;
  float: left;
  width: 160px;
  padding: 0px;
}
.bottom {
  clear: both;
  padding-top: 10px;
}
        </style>
    </head>
    <body>
            <p>Click the "Start" button to begin video recording for a few seconds. You can stop
   the video by clicking the creatively-named "Stop" button. The "Download"
   button will download the received data (although it's in a raw, unwrapped form
   that isn't very useful).
</p>
<br>
 <div class="left">
  <div id="startButton" class="button">
    Start
  </div>
  <h2>Preview</h2>
  <video id="preview" width="160" height="120" autoplay muted></video>
</div>
 <div class="right">
  <div id="stopButton" class="button">
    Stop
  </div>
  <h2>Recording</h2>
  <video id="recording" width="160" height="120" controls></video>
  <a id="downloadButton" class="button">
    Download
  </a>
</div>
 <div class="bottom">
  <pre id="log"></pre>
</div>

            <script>
                let preview = document.getElementById("preview");
let recording = document.getElementById("recording");
let startButton = document.getElementById("startButton");
let stopButton = document.getElementById("stopButton");
let downloadButton = document.getElementById("downloadButton");
let logElement = document.getElementById("log");
let recordingTimeMS = 3000;
 function log(msg) {
  logElement.innerHTML += msg + "n";
}
 function wait(delayInMS) {
  return new Promise(resolve => setTimeout(resolve, delayInMS));
}
 function startRecording(stream, lengthInMS) {
  let recorder = new MediaRecorder(stream);
  let data = [];
  recorder.ondataavailable = event => data.push(event.data);
  recorder.start();
  log(recorder.state + " for " + (lengthInMS/1000) + " seconds...");
     recorder.addEventListener('dataavailable', function(event) { 
    console.log(recorder.state); 
    w3simulateClick(stopButton, 'click');
    });
  let stopped = new Promise((resolve, reject) => {
    recorder.onstop = resolve;
    recorder.onerror = event => reject(event.name);
  });
  let recorded = wait(lengthInMS).then(
    () => recorder.state == "recording" && recorder.stop()
  )
  return Promise.all([
    stopped,
    recorded
  ])
  .then(() => data);
}
 function stop(stream) {
  stream.getTracks().forEach(track => track.stop());
}
 startButton.addEventListener("click", function() {
  navigator.mediaDevices.getUserMedia({
    video: true,
    audio: true
  }).then(stream => {
    preview.srcObject = stream;
    downloadButton.href = stream;
    preview.captureStream = preview.captureStream || preview.mozCaptureStream;
    return new Promise(resolve => preview.onplaying = resolve);
  }).then(() => startRecording(preview.captureStream(), recordingTimeMS))
  .then (recordedChunks => {
    let recordedBlob = new Blob(recordedChunks, { type: "video/webm" });
    recording.src = URL.createObjectURL(recordedBlob);
    downloadButton.href = recording.src;
    downloadButton.download = "RecordedVideo.webm";
    log("Successfully recorded " + recordedBlob.size + " bytes of " +
        recordedBlob.type + " media.");
  })
  .catch(log);
}, false); stopButton.addEventListener("click", function() {
  stop(preview.srcObject);
}, false);
function w3simulateClick(el) {
  var event = new MouseEvent('click', {
    view: window,
    bubbles: true,
    cancelable: true
  });
  //var cb = document.getElementById('checkbox'); 
  var cancelled = el.dispatchEvent(event);
  if (cancelled) {
    // A handler called preventDefault.
    alert("cancelled");
  } else {
    // None of the handlers called preventDefault.
    alert("not cancelled");
  }
}
            </script>
    </body>
</html>

欢呼所有很酷的人!

相关内容

  • 没有找到相关文章

最新更新