前端视频播放禁止快进

这个需求也是前端很常见的需求了,废话不多说,直接看代码,下面用vue3代码去演示

<templete>
  <div class="dialog-video">
  <video ref="videoPlayer" @timeupdate="timeupdate" autoplay controls muted object-fit="fill"
    x5-video-player-type="h5-page" x5-video-orientation="landscape|portrait" webkit-playsinline="true"
    playsinline="true">
    <source :src="current.videoUrl" type="video/mp4" />
  </video>
</div>
</templete>

<script setup>
  let lastCureentTime = 0;
//视频播放进度
const timeupdate = (e) => {
    let totalTimeSeconds = Math.round(e.target.duration);
    let currentTimeSeconds = Math.round(e.target.currentTime);
    // console.log("视频总时长(秒):", totalTimeSeconds)
    // console.log("视频播放进度(秒):", currentTimeSeconds);

    // console.log("lastCureentTime", lastCureentTime)
    const video = videoPlayer.value;

    //禁止用户快进代码
    if (currentTimeSeconds - lastCureentTime > 2) {
        console.log("用户快进")
        video.currentTime = lastCureentTime
    } else {
        lastCureentTime = currentTimeSeconds
        if (totalTimeSeconds == currentTimeSeconds) {
            //这里执行 视频自动播放完成后的逻辑 根据自己业务而定
            handleVideoEnded();
        }
    }
}
</script>

主要就是根据@timeupdate 这个视频监听的回调方法拿到播放进度的秒数 还有总时长

检测到用户拖动进度后 强制给他拉回上次拖动进度条前保存的进度

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值