0

function t() {
  var u = document.getElementById("fileupload").value;
  var video = document.getElementById('video');
  document.getElementById.scr = u;
  video.load();
  video.play();
};
<button id="submit" value="submit" onclick="t();">play</button>
<embed id="video" width="330" height="220" scr="">

4

1 回答 1

0

您无法使用其 src 或 id 直接读取或播放视频。

您必须执行以下操作才能完成您的要求。使用FileReader函数读取文件源input type file

<script>
    function t() {
      var preview = document.querySelector('embed');
      var video   = document.querySelector('input[type=file]').files[0];
      var reader  = new FileReader();

      reader.onloadend = function () {
        preview.src = reader.result;
      }

      if (video) {
        reader.readAsDataURL(video);
      } else {
        preview.src = "";
      }

      //video.load();
      //video.play();
    };
</script>

<input type="file" name="fileupload" value="fileupload" id="fileupload"> 
<label for="fileupload"> Select a file to play</label>

<embed id="video" width="330" height="220" scr="">
<button id="submit" value="submit" onclick="t();">play</button>

这是一个JSFiddle

于 2018-07-26T12:54:27.957 回答