本文使用vue-video-player组件,实现视频列表循环播放,同一个video画面中,循环播放视频列表,video
中options配置中的sources是数组,可以配置多个视频源,就可以通过代码实现sources列表中的资源进行
循环播放。

说明:转发请注明出处
在这里插入图片描述

安装 vue-video-player

npm i vue-video-player

.vue

<template>
  <div class="hello">
    <video-player ref="videoPlayer" class="vjs-custom-skin" :options="playerOptions" @ended="onPlayerEnded($event)">
    </video-player>
  </div>
</template>
<script>
import 'video.js/dist/video-js.css'
require('vue-video-player/src/custom-theme.css')
import { videoPlayer } from 'vue-video-player'
import videojs from 'video.js'
window.videojs = videojs
require('videojs-contrib-hls/dist/videojs-contrib-hls.js')
export default {
  name: 'HelloWorld',
  components: {
    videoPlayer
  },
  data() {
    return {
      playerOptions: {
        height: '360',
        playbackRates: [0.7, 1, 1.3, 1.5, 1.7],
        //需要循环的列表
        sourcesList: [
          {
            type: "video/mp4",
            src: "http://new.qingdao.gov.cn/masvod/public/2022/03/16/20220316_17f91c9e389_r1_600k.mp4"
          },
          {
            type: "video/mp4",
            src: 'https://api.dogecloud.com/player/get.mp4?vcode=5ac682e6f8231991&userId=17&ext=.mp4'
          }
        ],
        sources: [
          {
            type: "video/mp4",
            src: 'https://api.dogecloud.com/player/get.mp4?vcode=5ac682e6f8231991&userId=17&ext=.mp4'
          }
        ],
        controlBar: {
          timeDivider: true, // 当前时间和持续时间的分隔符
          durationDisplay: true, // 时长显示
          remainingTimeDisplay: true, // 剩下时间
          currentTimeDisplay: true, // 当前时间
          volumeControl: true, // 声音控制键
          playToggle: true, // 暂停和播放键
          progressControl: true, // 进度条
          fullscreenToggle: true // 全屏按钮
        },
        loop: false, //循环列表,这里的loop必须是false
        muted: true,// 是否在不全屏状态下外放声音
        autoplay: true,// 是否浏览器 准备好后自动播放
        isLive: false,// 是否直播
        flash: { hls: { withCredentials: false } },
        poster: "https://surmon-china.github.io/vue-quill-editor/static/images/surmon-5.jpg",
        currentSourceSrc: ''
      }
    }
  },
  mounted() {
    let { sources } = this.playerOptions;
    this.currentSourceSrc = sources.length ? sources[0].src : '';
  },
  methods: {
    // 视频播完回调
    onPlayerEnded(player) {
      let { sourcesList } = player.options_;
      let length = sourcesList.length;
      let currentSourceSrc = ''
      sourcesList.forEach((v, i) => {
        if (this.currentSourceSrc === v.src) {
          let index = i === length - 1 ? 0 : ++i
          console.log(index)
          this.$set(this.playerOptions, 'sources', [sourcesList[index]])
          this.$refs.videoPlayer.player.load()
        }
      })
      this.currentSourceSrc = currentSourceSrc;
    },
  }

}
</script>

说明:播放完一个视频之后,改变sources的内容,然后重新播放,在这里需要调用player.load(),而不是player.play()

Logo

前往低代码交流专区

更多推荐