Vue视频播放器 vue-video-player

Vue视频播放器 vue-video-player

本篇主要记录一下 在 vue 项目中 如何使用 vue-video-player 来做为视频播放器,因为最近在作博客的 轻松一刻模块 因此拿这个尝试一下 在此纪录一下 以及遇到的小坑等。。css

1. 安装 vue-video-player

直接输入 npm install 命令 注意不要使用 cnpmvue

npm install vue-video-player -S

2.配置 全局main.js文件 引入

直接在 vue 项目的 main.js 中 引入 插件和其样式npm

import VideoPlayer from 'vue-video-player'
require('video.js/dist/video-js.css')
require('vue-video-player/src/custom-theme.css')

Vue.use(VideoPlayer)

3.使用 vue-video-player

3.1 组件中引入

<video-player
        class="video-player vjs-custom-skin"
        ref="videoPlayer"
        :playsinline="true"
        :options="playerOptions">
</video-player>

3.2 编写 playerOptions

注意 必定要把 playerOptions 放入 computed 中去,不然会有报错问题!!浏览器

computed: {
    playerOptions() {
      const playerOptionsObj = {
        playbackRates: [0.7, 1.0, 1.5, 2.0], //播放速度
        autoplay: false, //若是true,浏览器准备好时开始回放。
        muted: false, // 默认状况下将会消除任何音频。
        loop: false, // 致使视频一结束就从新开始。
        preload: "auto", // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,当即开始加载视频(若是浏览器支持)
        language: "zh-CN",
        aspectRatio: "16:9", // 将播放器置于流畅模式,并在计算播放器的动态大小时使用该值。值应该表明一个比例 - 用冒号分隔的两个数字(例如"16:9"或"4:3")
        fluid: true, // 当true时,Video.js player将拥有流体大小。换句话说,它将按比例缩放以适应其容器。
        sources: [
          {
            type: "video/mp4",
            src: "https://www.askajohnny.com/shipinyasuo_1579414465.mov",
          },
          // {
          //   type: 'application/x-mpegURL',
          //   src: this.playUrl
        ],
        poster: "", //你的封面地址
        // width: document.documentElement.clientWidth,
        notSupportedMessage: "此视频暂没法播放,请稍后再试", //容许覆盖Video.js没法播放媒体源时显示的默认信息。
        controlBar: {
          timeDivider: true,
          durationDisplay: true,
          remainingTimeDisplay: false,
          fullscreenToggle: true, //全屏按钮
        },
      };
      return playerOptionsObj;
    },
  },

4.遇到的坑

  1. 注意 必定要把 playerOptions 放入 computed 中去,不然会有报错问题!!
  2. vue-video-player 依赖 video.js npm install vue-video-player 的时候 自动安装了 video.js 千万不要本身装 video.js 千万不要本身装 video.js 千万不要本身装 video.js 不然会冲突直接报错 pause 什么的
  3. 能够自定义样式//引入css require(‘vue-video-player/src/custom-theme.css’) require(’…/static/css/myvideo.css’) //若是你须要自定义播放器的样式,本身新建一个css

5. 效果图

image-20200811205306524

相关文章
相关标签/搜索