位置: IT常识 - 正文

video 自定义视频播放控件(videojs自定义按钮)

编辑:rootadmin
video 自定义视频播放控件

推荐整理分享video 自定义视频播放控件(videojs自定义按钮),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:video.js自定义样式,自定义video播放器,videojs自定义按钮,自定义video播放器,video自定义播放按钮,自定义video播放器,video自定义播放按钮,自定义video样式,内容如对您有帮助,希望把文章链接给更多的朋友!

ui设计的界面总是极具个性化的,要去修改插件中的视频控件的样式和布局太困难了,那就自己参照video原生事件,重写一个吧。

(效果图预览)

一、video标签的属性(props)

 html <video>标签 | 菜鸟教程

<video ref="videoPlayer" id="videoElement" controls autoplay :muted="isMute" width="800px" height="600px" > 您的浏览器不支持video</video>

参数说明:(更多属性参照上述菜鸟教程中的video标签)

controls:默认为true,即向用户展示视频控件(如播放、暂停按钮等)autoplay:如果出现该属性,则视频在就绪后马上播放。muted:是否静音,默认为truewidth:设置视频播放器的宽度二、video视频控件的触发事件 

video标签支持的多媒体事件(Media Events) | 菜鸟教程

<video ref="videoPlayer" id="videoElement" controls autoplay :muted="isMute" width="100%" height="100%" @loadeddata="setVideoPoster($event)" @progress="videoProgress($event)" @pause="videoPause($event)" @play="videoPlay($event)" @timeupdate="videoTimeUpdate()" @ended="videoEnded()" @contextmenu="contextmenu"> 您的浏览器不支持video</video><button @click="handlePlay">播放</button><button @click="handlePause">暂停</button><button @click="handleMute">切换静音</button><button @click="fullScreen">全屏</button>data(){ return{ isMute: true // 默认静音 }}1、播放(onplay事件)

this.$refs.videoPlayer.play();

methods:{ // 视频要开始播放时 videoPlay(e){ // ...触发该函数后视频会开始播放,我们可以做一些想做的事情,比如改变自定义播放按钮的样式等 }, // 自定义播放按钮中,触发视频的播放事件 handelPlay(){ this.$refs.videoPlayer.play(); // 会触发videoPlay()函数 }}2、暂停(onpause事件)

this.$refs.videoPlayer.pause();

methods:{ // 视频要暂停播放时 videoPause(e){ // ...触发该函数后视频会暂停播放 }, // 自定义播放按钮中,触发视频的播放事件 handelPause(){ this.$refs.videoPlayer.pause(); // 会触发videoPause()函数 }}3、静音(muted属性)

(1)切换静音

 // 手动切换静音(点击(非拖拽)静音时,用户选择的音量不变)

handleMute() {

video 自定义视频播放控件(videojs自定义按钮)

      this.isMute = !this.isMute;

},

(2)改变音量(volume属性)

this.$refs.videoPlayer.volume = a; (a为从 0~1的数字)

// 这里用element的进度条写音量大小调节条<el-slider v-model="curVolume" :show-tooltip="false" @input="changeVolume"></el-slider>data(){ return{ curVolume: 0, // 默认音量为0 }},methods:{ changeVolume(val){ this.curVolume = val; // 由于h5规定volum的值在0-1之间,所以这里要对获取到的val做一个处理(滑块的val是从0-100) this.$refs.videoPlayer.volume = val / 100; // 音量为0的时候,video控件为静音 if ( val == 0 ) { this.isMute = true; } else { this.isMute = false; } }}4、全屏fullScreen() { this.$refs.videoPlayer.webkitRequestFullScreen();},5、播放进度条

获取视频总时长(duration)

var videoObj = this.$refs.videoPlayer;

videoObj.addEventListener('canplay', () => {

        this.totalT = videoObj.duration;

})

获取视频加载进度

HTML5视频 - 加载百分比?

HTML5视频 - 加载百分比?

// 获取视频加载进度videoProgress(e){       var bf = this.$refs.videoPlayer.buffered;         var time = this.$refs.videoPlayer.currentTime;       if ( bf.length != 0 ){           var range = 0;           while( !( bf.start(range) <= time && time <= bf.end(range) ) ) {               range += 1;           }           var loadEndPercentage = ( bf.end(range) / this.playerVideo.duration ) * 100;  // 结束加载的百分比           this.persentLoad = loadEndPercentage;       }},

(1)父组件调用

<template> <video ref="videoPlayer" @progress="videoProgress($event)" @timeupdate="videoTimeUpdate()" > 您的浏览器不支持video </video> // 视频播放、加载进度条 <ProgressLine :presentT="presentT" :totalT="totalT" :persentLoad="persentLoad" @changeCurrentTime="changeCurrentTime($event)" @changeCurrentWord="changeCurrentWord($event)" > </ProgressLine> // 播放时长、视频总时长 <p> <span id="currentTime" ref="progressTimer">{{ videoCurrentTime }}</span> <span style="color: #ffffff;opacity: 0.3;">&nbsp;/&nbsp;</span> <span id="durationTime" ref="durationTimer">{{ videoTotalTime }}</span> </p></template>import ProgressLine from './ProgressLine.vue';export default { name: 'videoPage', components: { ProgressLine }, data(){ return{ presentT: 0, // 进度条的当前值,必须为number totalT: 0, // 进度条的最大值,必须为number persentLoad: 0, // 视频加载进度 videoCurrentTime: '00:00', // 当前视频已播放时长 videoTotalTime: '00:00', // 视频总时长 } }, methods:{ // 子组件传入的时间修改 changeCurrentTime(data) { this.$refs.videoPlayer.currentTime = data; // 点击进度条设置视频当前播放点 }, changeCurrentWord(data) { this.videoCurrentTime = this.formatTime(data); // 当前播放时间显示文字 }, // 获取视频加载进度 videoProgress(e){ var bf = this.playerVideo.buffered; var time = this.playerVideo.currentTime; if ( bf.length != 0 ){ var range = 0; while( !( bf.start(range) <= time && time <= bf.end(range) ) ) { range += 1; } var loadEndPercentage = ( bf.end(range) / this.playerVideo.duration ) * 100; // 结束加载的百分比 this.persentLoad = loadEndPercentage; } }, // 视频自动播放时 videoTimeUpdate(){ this.presentT = this.playerVideo.currentTime; // 获取当前播放时长 this.videoCurrentTime = this.formatTime(this.presentT); // 时间格式化 }, // 时间格式化 formatTime(t) { var m = parseInt(t % 3600 / 60) m = m < 10 ? '0' + m : m var s = parseInt(t % 60) s = s < 10 ? '0' + s : s return m + ':' + s }, }}

(2)进度条组件(播放进度条 和 加载进度条)

// ProgressLine.vue 进度条组件<template> <div> <div class="line-background"> <div class="time-line" @click="adjustProgress($event)"> <div class="progress-round" ref="progressRound"> <div class="loading" ref="persentLoad" style="width: 0;"></div> <!-- 加载进度条 --> <div class="progress" ref="progress" @click="adjustProgress"></div> <div class="round" ref="round" @mousedown="roundDrag"></div> </div> </div> </div> </div></template><script>export default { name: 'ProgressLine', props: { presentT: {}, totalT: {}, persentLoad: { default : 0 } }, data() { return { // 进度条拖拽 dragClick: false, // 鼠标/手指按下 clickDown: false, } }, created() { }, watch: { // 侦听当前播放时长设置进度条 presentT: { handler(newValue, oldValue) { // 未点击进度条 if (this.dragClick == false && this.clickDown == false) { this.$refs.progress.style.width = newValue / this.totalT * 100 + '%' if ((newValue / this.totalT * 100 - 1.23) < 0) { this.$refs.round.style.left = 0 + '%' } else { this.$refs.round.style.left = (newValue / this.totalT * 100) - 1.23 + '%' } } else if (this.dragClick == true) { this.dealWidth() this.dragClick = false } } }, persentLoad: { handler(newValue, oldValue) { this.$refs.persentLoad.style.width = ( newValue / 100 ) * 1300 + 'px'; } } }, methods: { progressData(data) { this.$emit('changeCurrentTime', data) this.$emit('changeCurrentWord', data) }, // 进度条位置和圆点定位处理 dealWidth() { this.$refs.progress.style.width = this.progressWidth / this.$refs.progressRound.offsetWidth * 100 + '%' if ((this.progressWidth / this.$refs.progressRound.offsetWidth * 100) - 1.23 < 0) { // 圆点定位 this.$refs.round.style.left = 0 + '%' } else { this.$refs.round.style.left = (this.progressWidth / this.$refs.progressRound.offsetWidth * 100) - 1.23 + '%' } }, // 进度条点击 adjustProgress(e) { this.dragClick = true e.preventDefault() const { left, width } = this.$refs.progressRound.getBoundingClientRect() // 进度条到屏幕距离及进度条的宽度 this.progressWidth = e.clientX - left if (this.progressWidth < 0) {//进度条边界值计算情况 this.progressWidth = 0 } else if (this.progressWidth >= width) { this.progressWidth = width } else { this.progressWidth = e.clientX - left // e.clientX:鼠标点击的位置到屏幕最左侧的距离 } this.dealWidth() this.progressData((this.progressWidth / width) * this.totalT) }, // 进度条圆点拖拽 roundDrag(event) { event.preventDefault() const offsetX = event.offsetX this.dragClick = true this.clickDown = true // 解决圆点拖拽进度条长度抖动 document.onmousemove = (e) => { // 给圆点添加移动事件 e.preventDefault()// 阻止进度条拖拽时屏幕原有的滑动功能 const X = e.clientX // 获取圆点离屏幕的距离 const { left, width } = this.$refs.progressRound.getBoundingClientRect() const ml = X - left // 进度条长度:圆点离屏幕的距离减去进度条最左边离屏幕的距离 if (ml <= 0) { // 进度条长度最小和最大值的界定 this.progressWidth = 0 } else if (ml >= width) { this.progressWidth = width } else { this.progressWidth = ml } this.progressData((this.progressWidth / width) * this.totalT) //视频播放时间 this.dealWidth() } // 抬起鼠标,结束移动事件 document.onmouseup = () => { document.onmousemove = null document.onmouseup = null this.clickDown = false } }, }}</script><style lang="less" scoped>.line-background { width: 100%; height: 10px; background-color: rgba(255, 255, 255, 0.3); .time-line { width: 100%; height: 10px; background-color: #565651; .progress-round { cursor: pointer; width: 100%; position: relative; display: flex; .loading { height: 10px; background-color: rgba(255, 255, 255, 0.3); } .progress { position: absolute; top: 0; left: 0; width: 00%; height: 10px; background-color: #3d7eff; } .round { position: absolute; top: 50%; left: 0; transform: translateY(-50%); width: 16px; height: 16px; border-radius: 16px; background: #ffffff; box-shadow: -2px 0px 2px 2px rgba(3, 0, 0, 0.30); } } }}</style>6、视频中禁用右键(可以禁止用户下载视频)// 在视频中禁用右键(禁止用户下载)contextmenu(e){ e.returnValue = false;},7、设置倍速播放

 this.$refs.videoPlayer.playbackRate = rate;   // rate 一般在[2.0,1.75,1.5,1.0,0.75,0.5]范围

本文链接地址:https://www.jiuchutong.com/zhishi/277613.html 转载请保留说明!

上一篇:Win10 19043.1237 9月累积更新 KB5005565推送(附更新修复+下载)

下一篇:苹果推送最新系统macOS Big Sur开发者预览版Beta 5(附推送内容)(苹果今天推送更新内容)

  • 如何申请一般纳税人
  • 银行开具的票据有哪些
  • 特许权使用费收入
  • 一般纳税人缴纳税金分录
  • 增值税进项税销项税
  • 费用分割单模板
  • 机械租赁带司机税目
  • 固定资产新建帐套
  • 收到现金货款可以直接用吗
  • 公司投资股票有风险吗
  • 企业出租自有厂房超经营吗
  • 代开专用发票缴纳的增值税需要计提吗?
  • 定额发票验旧怎么操作
  • 什么情况下要办居住证
  • 股份转让的溢价是什么意思
  • 金税盘年费如何做账
  • 行政单位库存物资管理办法
  • 本月不抵扣的发票不入帐吗
  • 税控盘管理费会计分录
  • 个人所得税的税收标准
  • 所得税预缴资产怎么计算
  • 加速折旧法和直线折旧法的区别
  • 收不到的物业费是否增值税确认收入
  • 生产车间工人发放福利
  • set up 和establish的区别
  • 公司招的兼职员工怎么报个税
  • 以房产投资入股应当缴纳契税
  • 无形资产相关税费
  • 【已解决】VUE3+webpack >5报错问题
  • 固定资产清理科目余额结转哪里
  • web网页设计期末作业猫眼电影首页
  • php去除字符串中的引号
  • vue的样式穿透
  • 自动驾驶决策规划技术理论与实践电子版
  • ssh -o命令
  • 健身房注册公司名称带超字
  • 预付工程款如何结算
  • python 索引-1
  • 脚手架租赁费用超过购买价格
  • 处置固定资产开票 税目
  • 债券承销费是指什么费用
  • 业务招待费的所得税扣除
  • 自产用于捐赠的会计处理
  • 利息收入填在汇算清缴哪里
  • 怎么计算土地增值税收入
  • 其它应付款是否可以抵扣
  • 协会申报材料
  • 发票已到材料未到会计分录
  • 免费赠送客户入群的文案
  • 当月只有进项税额会计怎么做账
  • 单独入账的土地为啥不提折旧
  • 一个工程项目多个业主吗
  • 固定资产折旧的会计科目
  • 销售部门交通费计入什么费用
  • 某酒店住宿费用定价分析
  • 案例解析企业所需资金
  • 租房买的中央空调怎么用
  • 商业企业可将商品分为哪三类
  • mysql5.7.
  • win10ie
  • ubuntu系统如何
  • Linux常用命令的实验总结
  • 怎样加快电脑开机速度
  • jquery 使用
  • jquery通过扩展select控件实现支持enter或focus选择的方法
  • js正则表达式gi
  • unity game optimization
  • jquery mobile app案例
  • vue如何处理跨域
  • js闭包的定义和用途
  • api/home/getmyregion
  • 彩票中奖归出钱人还是中奖人
  • 税务登记时必须要填银行账号吗
  • 经济适用房土地使用年限
  • 税务局取消办税人员
  • 税务上门核查要看什么
  • 源泉扣缴通俗
  • 免教育费附加会计分录
  • 什么是"五证合一"登记制度?办理"五证合一"程序和方案
  • 广西怎么查社保记录
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设