HTML, CSS, & JavaScript on Classic Google Sites - Why Video Recording Not Working?

旧街凉风 提交于 2020-06-29 06:41:32

问题


There is this code here that I am wanting to upload to Classic Google Sites. (The code is at the very bottom of the link that you can click on.)

I can get the HTML and CSS parts up and running to do the video recorder but the JavaScript part I have no idea why it is not working. The code goes in that order.

I can get some JavaScript to do other stuff but not this. I type in < script > "Copied and Pasted JavaScript Code" < /script> when using the code from the site linked above but no success.

What am I doing wrong? How do I get this JavaScript part up and running?

<!-- HTML -->
<html lang="en-US">
<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>
</html>
<!-- CSS -->
<script> //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;
}
</script>
<!-- JavaScript (Doesn't Work) -->
<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 = 5000;
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...");
 
  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);
</script>

回答1:


Alright so from what I can see, the code is actually right and without any issue, if you are putting it all in a single HTML file, you might want to add a head tag and then put the CSS styling in there inside a tag instead of a tag.

And Add the JS script inside the body tag at the end of the code, and it should run the code.

I have added the code that works below so you can compare it to your code and see where the difference is :)

    <html lang="en-US">
  <head>
    <style>
      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>
    <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 = 5000;
      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...");
      
        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);
    </script>
  </body>
</html>


来源:https://stackoverflow.com/questions/62469346/html-css-javascript-on-classic-google-sites-why-video-recording-not-workin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!