VUE使用video-player在线播放视频

韩麒
2023-12-01

1.安装

npm install vue-video-player --save

2.全局引用, 在main.js里面导入并引用

import VideoPlayer from “vue-video-player”;
import hls from “videojs-contrib-hls”;
require(“video.js/dist/video-js.css”);
require(“vue-video-player/src/custom-theme.css”);
Vue.use(VideoPlayer);

3.vue中使用

<template>
  <div class="video-warpper">
    <video-player
      class="video-player vjs-custom-skin"
      ref="videoPlayer"
      :options="playerOptions"
      :playsinline="true"
      @timeupdate="onPlayerTimeupdate($event)"
      @ready="playerReadied"
      @pause="onPlayerPause($event)"
    >
    </video-player>
  </div>
</template>
<script>
export default {
  name: "player",
  data() {
    return {
      playerOptions: {
        playbackRates: [0.5, 1.0, 1.5, 2.0], // 可选的播放速度
        autoplay: false, // 如果为true,浏览器准备好时开始回放。
        muted: false, // 默认情况下将会消除任何音频。
        loop: false, // 是否视频一结束就重新开始。
        // techOrder: ["html5"],
        // html5: { hls: { withCredentials: false } },
        preload: "auto", // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频(如果浏览器支持)
        language: "zh-CN",
        aspectRatio: "16:9", // 将播放器置于流畅模式,并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字(例如"16:9"或"4:3")
        fluid: true, // 当true时,Video.js player将拥有流体大小。换句话说,它将按比例缩放以适应其容器。
        sources: [
          {
            type: "", // 类型
            src: "" //require("@/assets/video/huamlan.mp4"), // url地址
          }
        ],
        poster: "", // 封面地址
        notSupportedMessage: "此视频暂无法播放,请稍后再试", // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
        controlBar: {
          timeDivider: true, // 当前时间和持续时间的分隔符
          durationDisplay: true, // 显示持续时间
          remainingTimeDisplay: true, // 是否显示剩余时间功能
          currentTimeDisplay: false, // 当前时间
          volumeControl: false, // 声音控制键
          playToggle: false, // 暂停和播放键
          progressControl: true, // 进度条
          fullscreenToggle: true // 是否显示全屏按钮
        }
      }, 
    };
  },

  computed: { 
  },
  mounted() { 
  },
  destroyed() {},
  methods: { 
    onPlayerPlay(player) {
      // console.log('player play!', player)
    },
    onPlayerPause(player) {
      console.log("player pause currentTime!", player.cache_.currentTime);
      let that = this;
      that.currentTime = player.cache_.currentTime;
    },
    onPlayerEnded(player) {
      // console.log('player ended!', player)
    },
    onPlayerLoadeddata(player) {
      // console.log('player Loadeddata!', player)
    },
    onPlayerWaiting(player) {
      // console.log('player Waiting!', player)
    },
    onPlayerPlaying(player) {
      // console.log('player Playing!', player)
    },
    /* 获取视频播放进度 */
    onPlayerTimeupdate(player) { 
    },
    /* 设置视频开始的进度 */
    playerReadied(player) { 
    },
    onPlayerCanplaythrough(player) {
      // console.log('player Canplaythrough!', player)
    },
    // or listen state event
    playerStateChanged(playerCurrentState) {
      // console.log('player current update state', playerCurrentState)
    }
  }
};
</script>
<style lang="scss">
.video-warpper {
  width: 100%;
  height: 100%; 
  .Videoindex {
    display: block;
    height: 350px;
    max-height: 350px;
    margin: 10px auto;
    .video-js .vjs-tech {
      width: 100%;
      height: 350px;
      max-height: 350px;
    }
  }

  .video-js .vjs-big-play-button {
    position: absolute;
    display: block;
    top: 50%;
    left: 50%;
    background: rgba(0, 0, 0, 0.7);
    border: none;
  }
  .video-js .vjs-big-play-button .vjs-icon-placeholder:before {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }
  .vjs-controls-disabled .vjs-big-play-button,
  .vjs-has-started .vjs-big-play-button,
  .vjs-using-native-controls .vjs-big-play-button,
  .vjs-error .vjs-big-play-button {
    display: none;
  }
  .vjs-paused .vjs-big-play-button {
    display: block;
  }
}
</style> 

4.## 使用Nginx配置视频快进/快退

add_header Accept-Ranges bytes;

 类似资料: