video.js实现鼠标移入播放及根据鼠标移动位置播放对应帧及进度条显示


这段时间都是自主学习,有些无聊了,在我的强烈要求下我师父让我做一个类似于B站视频预览的效果,效果如下图,然后让我看video.js的文档来实现,现在做一个总结。
在这里插入图片描述

video.js安装及使用

npm  install video.js
//在main.js中引入
import Video from 'video.js'
import 'video.js/dist/video-js.css'
Vue.prototype.$video = Video //引入Video播放器
//使用以及定义
<template>
  <video ref="videoPlayer" class="video-js"></video>
</template>
data() {
    return {
      player: null,
      loading: true,
      videoOptions: {
        // autoplay: "muted", //自动播放
        autoplay: false,
        controls: false, //用户可以与之交互的控件
        loop: true, //视频一结束就重新开始
        muted: true, //默认情况下将使所有音频静音
        aspectRatio: '16:9', //显示比率
        poster: '',
        fullscreen: {
          options: { navigationUI: 'hide' },
        },
        sources: '',
      },
    }
  },
  created() {
    this.videoOptions.sources = this.url//定义视频来源
    this.videoOptions.poster = this.poster //这个poster是父组件传过来的
  },
  mounted() {
    let _this = this
    //onPlayerReady方法是为了视频播放不了的时候不显示错误的信息
    this.player = this.$video(this.$refs.videoPlayer, this.videoOptions, function onPlayerReady() {
      this.on('error', function() {
        this.errorDisplay.close_() //将错误信息不显示
      })
      this.on('canplaythrough', function() {
        _this.loading = false
      })
    })
  },

鼠标移入播放移出停止播放

    handleMouseEnter() {
      if (this.player) {
        this.player.play()
      }
      this.$emit('videoTime', this.player.duration())
    },
    handleMouseLeave() {
      if (this.player) {
        this.player.pause()
      }
    },

根据鼠标移动位置播放对应帧及进度条显示

思路:根据鼠标在video中悬停的位置所占整个video宽度的百分比来显示当前视频的播放帧,进度条也一样的思路。
具体实现:
currentTime:当前播放的所在位置
duration:video的总时长
getBoundingClientRect:显示一个元素距离浏览器多少距离的集合

    mouseMove(event) {//这个event是接收父组件传过来的鼠标位置
      var videoleft=this.$refs.videoPlayer.getBoundingClientRect().left//video插件距离浏览器左边的距离
      var clientx = event.clientX //鼠标距离浏览器左边的距离
      var videoWidth = this.$refs.videoPlayer.clientWidth //div总宽度
      var ratio = (clientx-videoleft) / videoWidth //当前鼠标移动的比率
      var videoduration = this.player.duration() //总时长
      this.player.currentTime(videoduration * ratio)
      this.$emit('videoratio', ratio)
    },

进度条实现

<!-- 进度条 -->
<span class="item-box-view-progress" v-show="item.Type == 1 && item.showPlay && item.showModal">
<el-row class="item-box-view-progress-ratio">
<el-col :span="videoplayerprogress"><div class="item-box-view-progress-ratio-arrive"></div></el-col>
<el-col :span="24 - videoplayerprogress"
><div class="item-box-view-progress-ratio-surplus"></div
></el-col>
</el-row>
</span>

<VideoPlayer
:poster="item.Mate ? item.Mate.picUrl : item.Thumbnail"
:url="item.PlayUrl"
:mute="item.mute"
:play="item.play"
:ref="'videoPlayer' + index"
@videoratio="videoratio"
></VideoPlayer>

videoplayerprogress: 0, //预览播放进度

    videoratio(value) {
      console.log(value)
      this.videoplayerprogress = Math.round(24 * value)
    },

PS:以上就完成了,这里的话是在父组件中通过ref调用子组件的mouseMove的方法,其实也可以都在子组件中去写,不需要用到父组件,因为是用了getBoundingClientRect这个方法,先获取到元素在浏览器中距离左侧的距离,然后获取鼠标在浏览器中距离左侧的距离,两个相减就是鼠标在元素中距离左侧的距离,然后除于div的宽度就可以了。这里一开始是因为video元素中还有2个div,鼠标经过的时候判断距离元素左侧是用的offsetx,这样就会判断成子元素的offsetx就不对了,后来才改用的getBoundingClientRect方法来做,通过父组件调用子组件也能跑就没去改了。

  • 8
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
要在video.js实现播放列表,你可以按照以下步骤进行操作: 1. 首先,确保你已经引入了video.js库到你的项目中。你可以通过在HTML文件中添加以下代码来引入video.js: ```html <link href="https://unpkg.com/video.js/dist/video-js.min.css" rel="stylesheet"> <script src="https://unpkg.com/video.js/dist/video.min.js"></script> ``` 2. 创建一个包含视频播放器的HTML元素。例如,你可以在HTML文件中添加以下代码来创建一个video标签: ```html <video id="my-video" class="video-js vjs-default-skin" controls preload="auto" width="640" height="264"></video> ``` 3. 初始化video.js播放器。你可以在JavaScript文件中添加以下代码: ```javascript var myPlayer = videojs('my-video'); ``` 4. 创建一个包含视频源和标题的播放列表数组。例如,你可以在JavaScript文件中添加以下代码: ```javascript var playlist = [ { src: 'video1.mp4', title: 'Video 1' }, { src: 'video2.mp4', title: 'Video 2' }, // 添加更多的视频... ]; ``` 5. 添加逻辑来切换播放列表中的视频。你可以在JavaScript文件中添加以下代码: ```javascript var currentVideoIndex = 0; function playNextVideo() { currentVideoIndex = (currentVideoIndex + 1) % playlist.length; var video = playlist[currentVideoIndex]; myPlayer.src(video.src); myPlayer.poster(''); // 如果你有自定义的封面图片,可以在这里设置 myPlayer.play(); } myPlayer.on('ended', playNextVideo); ``` 6. 最后,调用`playNextVideo()`函数来开始播放第一个视频: ```javascript playNextVideo(); ``` 通过按照上述步骤,你就可以在video.js实现一个简单的播放列表了。记得根据你的需求修改视频源和标题,以及自定义播放器的样式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值