web端实现视频播放(进度条更新,禁止快进)

实现效果:


视频容器:
<div class="content">
<video
  autoplay
  @ended="videoEnded"
  @timeupdate="videoUpdate"
  ref="video"
  width="100%"
  controls
  disablePictureInPicture
  controlsList="nodownload noremoteplayback disablePictureInPicture noplaybackrate"
  :src="videoUrl"></video>
​​​​​​​</div> 

方法:


    // 点击章节展开按钮
    handleOn (list, item) {
      list.forEach(i => { i.active = false })
      item.active = true
      this.getCourseInfo(item).then((chiList) => {
        this.clickChi(chiList, chiList[0])
      })
    },   
    // 获取章节列表
    getCourseInfo (item) {
      return new Promise((resolve, reject) => {
        this.$api.workbenchesPeixun.courseInfo({ courseId: item.id, pxId: this.pxId }).then(res => {
          item.children = res.sectionInfoVOS.map(_item => {
            return {
              ..._item,
              currentTime: _item.lastView,
              courseId: item.id,
              active: this.selectedVideo ? this.selectedVideo.id === _item.id : false
            }
          })
          resolve(item.children)
        }).catch(err => {
          reject(err)
        })
      })
    },
    // 点击章节项
    clickChi (list, item) {
      list.forEach(i => { i.active = false })
      item.active = true
      this.selectedVideo = item
      this.videoUrl = `${process.env.VUE_APP_BASE_API}/px/online/play?videoId=${item.videoId}&token=${this.$store.state.token}`
      this.$refs.video.load()
      this.$refs.video.currentTime = item.lastView //视频定位到已播放的位置
    },
// 视频播放结束
    videoEnded: _.throttle(function () {
      this.updateRecord(this.selectedVideo.videoTime, 1)
    }, 500),
    // 观看中
    videoUpdate: _.throttle(function () {
      if (this.selectedVideo.ended) {
        // 跟上次观看时间相差30s
        if (Math.floor(this.$refs.video.currentTime) - this.selectedVideo.lastView > 29) {
          this.updateRecord(Math.floor(this.$refs.video.currentTime), 1)
        }
      } else {
        // 还没看完视频
        if (Math.floor(this.$refs.video.currentTime) - this.selectedVideo.currentTime > 1) {
          this.$message.warning('未学习完不能快进')
          this.$refs.video.currentTime = this.selectedVideo.currentTime
        } else {
          // 正常观看重置currentTime,快退不管
          if (Math.floor(this.$refs.video.currentTime) > this.selectedVideo.currentTime) {
            this.selectedVideo.currentTime = Math.floor(this.$refs.video.currentTime)
          }
          // 跟上次观看时间相差30s
          if (Math.floor(this.$refs.video.currentTime) - this.selectedVideo.lastView > 29) {
            this.updateRecord(Math.floor(this.$refs.video.currentTime))
          }
        }
      }
    }, 500),
//更新进度条(修改观看记录)
    updateRecord (viewTime = 0, ended = 0) {
      const data = {
        courseId: this.selectedVideo.courseId,
        ended: ended,
        pxId: this.pxId,
        sectionId: this.selectedVideo.id,
        viewTime: viewTime
      }
      this.$api.workbenchesPeixun.addViewRecord(data).then(res => {
        const item = this.courseList.filter(i => i.id === data.courseId)[0]
        this.selectedVideo.lastView = viewTime
        this.selectedVideo.currentTime = viewTime
        this.selectedVideo.ended = ended
        this.getCourseInfo(item)
        if (ended === 1) {
          this.getInfo()
        }
      })
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值