【问题标题】:video is not loading, what is wrong in the code视频没有加载,代码有什么问题
【发布时间】:2019-01-03 10:09:47
【问题描述】:

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="">

【问题讨论】:

  • 您显示的代码中没有 ID 为 "fileupload" 的 元素。
  • 你确定这个document.getElementById.scr = u是正确的吗?
  • 跨度>
  • @marekful 确实,似乎应该是 src 而不是 scr。
  • 我用过这个

标签: javascript html


【解决方案1】:

您无法使用其 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

【讨论】:

  • 感谢您的回复,即使它不起作用,您能否提供其他播放视频的方式
  • 感谢您的回复,我仍然面临同样的问题。我无法播放视频。
  • 问题是我可以在 Firefox 中播放视频,但在 ie 和 chrome 中显示错误
  • stackoverflow.com/questions/4100927/chrome-filereader 这可能有助于在 chrome 或 IE 中处理代码
猜你喜欢
  • 2011-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多