设置视频源时没有loadedmetadata回调

问题描述 投票:0回答:2

我需要通过输入标签上传的视频的视频尺寸。但是当我将上传的视频设置为视频标记的源时,不会调用loadedmetadata事件。

在这个方法中我设置了视频和监听器:

function getVideoDimensionsOf(objUrl){
    return new Promise(function(resolve){
        let video = document.createElement('video');
        //THIS GETS CALLED AS EXPECTED
        video.addEventListener( "loadedmetadata", function () {
            //THIS GETS NEVER CALLED
            let height = this.videoHeight;
            let width = this.videoWidth;
            console.log(height,width)
        }, false );
        video.src = objUrl;
    });
}

在此方法中,我设置了视频上传的回调:

function localFileVideoPlayer() {
  var URL = window.URL || window.webkitURL
  var uploadSelectedFile = function (event) {
    var file = this.files[0]
    var type = file.type
    var fileURL = URL.createObjectURL(file)
    var fileReader = new FileReader();
    fileReader.onload = function() {
        var videofile = this.result;
        //do something here with video data
    };
    fileReader.readAsArrayBuffer(file);
    getVideoDimensionsOf(window.URL.createObjectURL(file))//-->>HERE I CALL THE FUNCTION THAT SHOULD SET THE VIDEO SOURCE
  }
  var inputNode = document.getElementById("videofile")
  inputNode.addEventListener('change', uploadSelectedFile, false)
}

这是html上传字段:

<div>
    Upload Video:
    <input id="videofile" type="file" accept="video/*"/>
</div>

我检查了getVideoDimensionsOf方法被调用,但为什么loadedmetadata监听器没有得到回调?

javascript html html5-video blob
2个回答
0
投票

因为没有任何东西强迫浏览器预加载您的视频元素。据他所知,你永远不会播放这个视频,所以它不会预先加载它的内容。

您可以尝试通过调用其play()方法强制执行此预加载。

function getVideoDimensionsOf(objUrl) {
  return new Promise(function(resolve) {
    let video = document.createElement('video');
    video.muted = true; // bypass Chrome autoplay policies
    video.addEventListener("loadedmetadata", function() {
      let height = this.videoHeight;
      let width = this.videoWidth;
      video.pause();
      resolve( { width, height } );
    }, false);
    video.src = objUrl;
    video.play();
  });
}

inp.onchange = e => {
  const url = URL.createObjectURL(inp.files[0]);
  getVideoDimensionsOf(url)
    .then(obj => {
      URL.revokeObjectURL(url);
      console.log(obj);
    });
}
<input type="file" id="inp" accept="video/*,.mp4">

-1
投票

localFileVideoPlayer();

function getVideoDimensionsOf(objUrl) {
  return new Promise(function(resolve) {
    let video = document.createElement("video");
    {
      // if you need to append the video to the document
      video.controls = true;
      document.body.appendChild(video);
    }
    //THIS GETS CALLED AS EXPECTED
    video.addEventListener(
      "loadedmetadata",
      function() {
        //THIS GETS NEVER CALLED
        let height = this.videoHeight;
        let width = this.videoWidth;
        console.log(height, width);
      },
      false
    );
    video.src = objUrl;
  });
}
function localFileVideoPlayer() {
  var URL = window.URL || window.webkitURL;
  var uploadSelectedFile = function(event) {
    var file = this.files[0];
    var type = file.type;
    var fileURL = URL.createObjectURL(file);
    var fileReader = new FileReader();
    fileReader.onload = function() {
      var videofile = this.result;
      //do something here with video data
    };
    fileReader.readAsArrayBuffer(file);
    getVideoDimensionsOf(window.URL.createObjectURL(file)); //-->>HERE I CALL THE FUNCTION THAT SHOULD SET THE VIDEO SOURCE
  };
  var inputNode = document.getElementById("videofile");
  inputNode.addEventListener("change", uploadSelectedFile, false);
}
<div>
  Upload Video:
  <input id="videofile" type="file" accept="video/*" />
</div>

在您的代码中,您需要使用localFileVideoPlayer方法初始化输入更改事件,因此我首先执行此方法。然后,如果您需要在文档中显示此视频,则需要在文档中附加视频元素,因此getVideoDimensionsOf中有appendChild方法

© www.soinside.com 2019 - 2024. All rights reserved.