js获取视频文件的分辨率问题:
关键三步:
1、监听video标签dom元素的loadedmetadata或canplay事件;
2、获取videoWidth和videoHeight的值;
3、异步监听,同步返回,利用return new Promise()方法。
话不多说,直接上代码
<-- html代码 -->
<div class="box_video">
<video id="video_destiny" ref="video_destiny" autoplay src="/media/video/tiankong.mp4" muted="true" loop="true"></video>
</div>
//js代码,结合了vue框架
mounted() {
this.getVideoData().then(videoInfo => {
console.log("videoInfo对象:",videoInfo);
});
}
methods: {
getVideoData(){
return new Promise((resolve,reject)=>{
let videoElement = document.querySelector('video');
videoElement.addEventListener('loadedmetadata', function (){
resolve({
width: this.videoWidth,
height: this.videoHeight,
duration: this.duration,
});
});
});
},
}
输入结果:
壁纸放送: