Getting the real HTML5 video width and height

前端 未结 4 1464
鱼传尺愫
鱼传尺愫 2021-01-31 18:50

I have a video element:

var video = window.content.document.createElement(\"video\");
video.width = width;
video.height = height; 
video.style.backgroundColor =          


        
4条回答
  •  萌比男神i
    2021-01-31 19:12

    You should add a loadeddata event listener to the video, and try to read the size then, which is when enough information about the stream has been decoded and the dimensions can be accurately determined.

    Still, it sometimes takes a bit in some cases to get the dimensions ready, so you might want to try several times (with delay) until it works.

    That might be why it is not working for you but it's working for Sam.

    Here's how I check the video size, with several attempts if required, in my gumhelper library: https://github.com/sole/gumhelper/blob/master/gumhelper.js#L38

    Notice how we try several times and if it doesn't work we "give up" and default to 640x480.

提交回复
热议问题