时间:2022-11-25 10:41:32 | 栏目:vue | 点击:次
传入参数为视频文件对象
js的代码如下:
getVideoDuration(file) { var url = URL.createObjectURL(file); var audioElement = new Audio(url); var self = this; var result; audioElement.addEventListener("loadedmetadata", function() { // 视频时长值的获取要等到这个匿名函数执行完毕才产生 result = audioElement.duration; //得到时长为秒,小数,182.36 self.ruleForm.videoDuration = parseInt(result); //转为int值 }); }
npm install vue-video-player -S
import VideoPlayer from 'vue-video-player' import 'vue-video-player/src/custom-theme.css' import 'video.js/dist/video-js.css' Vue.use(VideoPlayer)
import { videoPlayer } from 'vue-video-player' import 'video.js/dist/video-js.css' export default { components: { videoPlayer } }
<video-player class="video-player vjs-custom-skin" ref="videoPlayer" :playsinline="true" :options="playerOptions"> </video-player>
this.$nextTick(() => { setTimeout(() => { let du = document.getElementById("vjs_video_3_html5_api") //获取组件下的video var hour = parseInt((du.duration) / 3600); var minute = parseInt((du.duration % 3600) / 60); var second = parseInt(du.duration % 60); let result = '' if (hour > 0) { result = this.formatTimeStr(hour) + ':' + this.formatTimeStr(minute) + ':' + this .formatTimeStr(second) } else { result = this.formatTimeStr(minute) + ':' + this.formatTimeStr(second) } return result //转化成分,秒 }, 200) }) formatTimeStr(val) { if (val > 9) { return val } else { return '0' + val } },