Video.js 使用教程 - 手把手教你基于 Vue 搭建 HTML 5 视频播放器

本文首发:《Video.js 使用教程 - 手把手教你基于 Vue 搭建 HTML 5 视频播放器

Video.js 是最强大的网页嵌入式 HTML 5 视频播放器的组件库之一,也是大多数人首选的网页视频播放解决方案。复杂的网页视频渲染,在引入 Video.js 后,轻松解决。本教程手把手教你搭建一套基于 Vue 的 Video.js 视频播放页。

跟随本教程学习,最终你可以自己搭建一套可以播放本地视频文件及网络流媒体的网页视频播放器。学习如何修改 video.js 的默认样式来实现播放按钮自定义形状(圆形)、居中及播放时间的显示与否, 如何播放 m3u8 格式,以及如何使用 video 的属性、事件及方法,音量增减,最终实现一个功能齐全的视频播放器。

video-fulfilled

跟随本教程学习,搭建的最终 video.js HTML5 视频播放效果。

另外,这个世界已经悄然发生变化,现在根本无需写任何前端代码,直接使用卡拉云 —— 新一代低代码开发工具帮你搭建后台工具,卡拉云可一键接入常见数据库及 API ,无需懂前端,内置完善的各类前端组件,无需调试,拖拽即用。原来三天的工作量,现在 1 小时搞定,谁用谁知道,用上早下班,详见本文文末。

配置 Vue 环境 - 基础部分

通过 npm 安装 Vue 脚手架 vue-cli

npm install -g @vue/cli

然后创建一个 Vue 项目 kalacloud-vue-video

vue create kalacloud-video

选择合适的选项后,安装完成,通过 cd 命令进入 kalacloud-vue-video 目录(此目录为我们的主开发目录),使用 npm run serve 命令,可以将项目运行起来。

npm-success

在 Vue 中使用 videojs

首先使用 npm 安装 video.js

npm i video.js

安装完毕后,在 main.js 中进行引入

import videojs from "video.js";
import "video.js/dist/video-js.css";
Vue.prototype.$video = videojs;

为了代码复用性,我们来创建一个 PlayerVideo 组件。在组件中我们分别定义 template 部分和 script 如下

最开始我们选用默认源链接: https://playtv-live.ifeng.com/live/06OLEGEGM4G.m3u8

<!-- controls:向用户显示播放按钮控件 -->
<template>
  <video
    ref="video"
    class="video-js vjs-default-skin"
    width="600"
    height="400"
    controls
  >
    <source src="https://playtv-live.ifeng.com/live/06OLEGEGM4G.m3u8" />
  </video>
</template>
export default {
  data() {
    return {
      player: null, // 用来存储当前 video
    };
  },
  mounted() { // 渲染视频
    this.player = this.$video(this.$refs.video);
  },
};

然后删除掉初始化 vue 项目默认的 App.vue 内代码,将 PlayerVideo 组件添加到 App 中,并调整播放器至中间。

<template>
  <div id="app">
    <div class="video-content">
      <player-video :src="src"></player-video>
    </div>
  </div>
</template>
<script>
  import PlayerVideo from "./components/PlayerVideo.vue";
  export default {
    components: {
      PlayerVideo,
      data() {
        return {
          src: "http://vjs.zencdn.net/v/oceans.mp4",
        };
      },
    },
  };
</script>
<style lang="scss">
  #app {
    font-family: Avenir, Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;

    .video-content {
      display: flex;
      justify-content: center;
      flex-direction: column;
      align-items: center;
    }
  }
</style>

在 kalacloud-vue-video 根目录使用 npm 运行下列命令:

npm run serve

在浏览器打开 http://localhost:8080/ 就可以成功渲染视频。

video-basic

我们大致的来看一下目前视频播放器拥有的功能:

  • 播放与暂停功能(目前播放按钮位于左上角)
  • 可以调节音量
  • 支持全屏与小屏播放

同样我们也可以发现一些不符合日常习惯的地方:

  • 播放按钮通常位于中间
  • 播放按钮一般为圆形
  • 暂停时会显示播放按钮

下面我们就讲述一些 tips 优化一下播放器。

扩展阅读:《最好用的 6 款 Vue 拖拽组件库推荐

如何使 Video.js 播放按钮垂直居中

将播放按钮垂直居中非常容易实现,video 官方提供了 vjs-big-play-centered。给 <video> 标签添加 vjs-big-play-centered 类名就可以实现播放按钮垂直居中。

<video class="video-js vjs-default-skin vjs-big-play-centered"></video>

video-basi-center

如何修改 Video.js 播放按钮为圆形

修改播放按钮为圆形需要修改对应类名的 CSS 样式。我们在 PlayerVideo 组件的 style 中添加下列样式代码。

修改时属性必须设置为 !important ,否则不会生效。

.video-js .vjs-big-play-button {
  font-size: 2.5em !important;
  line-height: 2.3em !important;
  height: 2.5em !important;
  width: 2.5em !important;
  -webkit-border-radius: 2.5em !important;
  -moz-border-radius: 2.5em !important;
  border-radius: 2.5em !important;
  background-color: #73859f;
  background-color: rgba(115, 133, 159, 0.5) !important;
  border-width: 0.15em !important;
  margin-top: -1.25em !important;
  margin-left: -1.75em !important;
}
.vjs-big-play-button .vjs-icon-placeholder {
  font-size: 1.63em !important;
}

video-basic-circle

扩展阅读:《最好用的 12 款 Vue Timepicker 时间日期选择器测评推荐

如何修改 Video.js 暂停时显示播放按钮

这个功能同样可以通过修改 CSS 实现。在 PlayerVideo 组件的 style 中添加下列样式代码。

.vjs-paused .vjs-big-play-button,
.vjs-paused.vjs-has-started .vjs-big-play-button {
  display: block !important;
}

video-basic-pause

如何设置 Video.js 显示当前播放时间

通过修改两个类的状态可以实现显示播放时间的功能,在 PlayerVideo 组件中设置下列样式代码:

.video-js .vjs-time-control {
  display: block !important;
}
.video-js .vjs-remaining-time {
  display: none !important;
}

video-basic-playtime

扩展阅读:《vue.draggable 入门指南 - 手把手教你开发任务看板

进阶使用部分

基础部分我们使用 this.$video(this.$refs.video) 渲染出播放器,但 this.$video 本质上是 video.js 提供的 videojs 函数,videojs 函数共有三个参数,第一个参数是绑定播放器的元素,第二参数为 options 对象,提供播放器的配置项,第三个参数为播放器渲染后的回调函数。

我们给 PlayerVideo 组件的 data 添加 options 对象,并设置 controls 为 false,同时设定一个简单的回调函数。

controls 属性是用来控制播放器是否具有与用户交互的控件——播放按钮等。如果设置为 false ,播放器将不显示播放控件,那么视频只能通过 Player API 或者 autoplay 控制播放。

export default {
  data() {
    return {
      player: null,
      options: { controls: false },
    };
  },
  mounted() {
    this.player = this.$video(this.$refs.video, this.options, () => {
      alert("播放器渲染完成");
    });
  },
};

video-advance-try-param

我们可以发现,播放器渲染完成后,浏览器发出了通知,并且播放器上没有控件出现。

更多的配置项链接: video-options

Video.js 常用事件

video 提供了很多常用事件,我们可以通过监测事件来处理不同的逻辑。

例如监测 play/pause 事件,给用户发送提醒

修改 PlayerVideo 组件中的 mounted 方法:

mounted() {
  this.player = this.$video(this.$refs.video, this.options, function () {
    this.on("play", () => {
      alert("视频已经开始播放,祝你有好的体验");
    });
    this.on("pause", () => {
      alert("视频已经暂停");
    });
  });
}

video-improve-event

扩展阅读:《Vue 实现 PDF 文件在线预览 - 手把手教你写 Vue PDF 预览功能

Video.js 音量记忆功能

为了让大家更能理解监听事件的用处,我们举一下实际的案例: 音量记忆功能。

为了更好的用户体验,用户每次调整音量后,我们应该帮其记住当前音量。当用户刷新页面或者重新进入页面后,无需再次调整音量。

这个功能其实不难实现:

  • 监听 volumechange 事件,当用户修改音量时,把此音量存储到 localStorage 中(如果音量功能会有多个组件使用,建议同时存放在 Vuex 中)
  • 当页面刷新或进入页面后,从 localStorage 中取出音量值,同步设置播放器音量。

我们修改一下 PlayerVideo 组件,使其可以接受属性 volume 音量值,同时添加事件 volumechange 和 play 事件的监听。

当 volumechange 触发时,将当前音量值存储到 localStorage 中;当 play 事件触发时,更新音量值。

<template>
  <video
    ref="video"
    controls
    class="video-js vjs-default-skin vjs-big-play-centered"
    width="600"
    height="400"
  >
    <source src="https://playtv-live.ifeng.com/live/06OLEGEGM4G.m3u8" />
  </video>
</template>

<script>
  export default {
    // 接受App传值
    props: ["volume"],
    data() {
      return {
        player: null,
        // volumnVideo 记录音量
        volumeVideo: this.volume,
      };
    },
    mounted() {
      const _this = this;
      this.player = this.$video(this.$refs.video, this.options, function () {
        this.on("volumechange", () => {
          // 存储音量
          _this.volumeVideo = this.volume();
          window.localStorage.volume = this.volume();
        });
        this.on("play", () => {
          this.volume(this.volumeVideo);
        });
      });
    },
  };
</script>

同时在 App.vue 中使用全局钩子函数 beforeCreate 获取到 localStorage 中存取的 volume

beforeCreate() {
  this.volume = window.localStorage.volume;
},

通过上述步骤,就可以成功实现音量记忆功能。

video-improve-volume

扩展阅读:《顶级好用的 8 款 Vue 弹窗组件测评与推荐

Video.js 简单视频播放器搭建

下面我带大家实现一下播放器的各种控制方法: 开始、暂停、重新加载、快进、后退、增大音量、降低音量以及换台功能。

video.js 对于这些控制方法都对应提供了方法。我们只需对提供的方法略作封装,即可使用。

下面我们就利用 video.js 提供的方法实现一个简单的播放器功能。

我们分别修改 PlayerVideo 组件和 App 组件如下:

PlayerVideo.vue 代码如下:

<template>
  <video
    ref="video"
    controls
    class="video-js vjs-default-skin vjs-big-play-centered"
    width="600"
    height="400"
  >
    <source :src="src" />
  </video>
</template>

<script>
  export default {
    props: ["volume", "src"],
    data() {
      return {
        player: null,
        volumeVideo: this.volume,
      };
    },
    methods: {
      // 封装播放器方法
      play() {
        this.player.src({ src: this.src });
        this.player.load(this.src);
        this.player.play(this.volumeVideo);
      },
      stop() {
        this.player.pause();
      },
      reload() {
        this.stop();
        this.player.load({});
        this.play();
      },
      forward() {
        const currentTime = this.player.currentTime();
        this.player.currentTime(currentTime + 5);
      },
      back() {
        const currentTime = this.player.currentTime();
        this.player.currentTime(currentTime - 5);
      },
      volumeUp() {
        this.player.volume(this.volumeVideo + 0.1);
      },
      volumeDown() {
        this.player.volume(this.volumeVideo - 0.1);
      },
      toggleTv(obj) {
        this.player.src(obj.src);
        this.player.load(obj.load);
        this.player.play(this.volumeVideo);
      },
    },
    mounted() {
      const _this = this;
      this.player = this.$video(this.$refs.video, this.options, function () {
        this.on("volumechange", () => {
          // 存储音量
          _this.volumeVideo = this.volume();
          window.localStorage.volume = this.volume();
        });
        this.on("play", () => {
          this.volume(this.volumeVideo);
        });
      });
    },
  };
</script>

<style>
  .video-js .vjs-time-control {
    display: block !important;
  }
  .video-js .vjs-remaining-time {
    display: none !important;
  }

  .video-js .vjs-big-play-button {
    font-size: 2.5em !important;
    line-height: 2.3em !important;
    height: 2.5em !important;
    width: 2.5em !important;
    -webkit-border-radius: 2.5em !important;
    -moz-border-radius: 2.5em !important;
    border-radius: 2.5em !important;
    background-color: #73859f;
    background-color: rgba(115, 133, 159, 0.5) !important;
    border-width: 0.15em !important;
    margin-top: -1.25em !important;
    margin-left: -1.25em !important;
  }
  .vjs-big-play-button .vjs-icon-placeholder {
    font-size: 1.63em !important;
  }

  .vjs-paused .vjs-big-play-button,
  .vjs-paused.vjs-has-started .vjs-big-play-button {
    display: block !important;
  }
</style>

App.vue 代码如下:

<template>
  <div id="app">
    <div class="video-content">
      <h2>使用video.js 在网站中搭建视频</h2>
      <h3>卡拉云——低代码开发工具,1 秒搭建上传后台</h3>
      <player-video :volume="volume" ref="video" :src="src"></player-video>
    </div>
    <div class="button-group">
      <el-button class="primary" @click="playVideo">开始视频</el-button>
      <el-button class="primary" @click="stopVideo">暂停视频</el-button>
      <el-button class="primary" @click="reloadVideo">重新加载</el-button>
      <el-button class="primary" @click="forwardVideo">视频快进</el-button>
      <el-button class="primary" @click="backVideo">视频后退</el-button>
      <el-button class="primary" @click="volumeUpVideo">增大音量</el-button>
      <el-button class="primary" @click="volumeDownVideo">降低音量</el-button>
      <el-button class="primary" @click="toggleToFenghuangwang"
        >凤凰卫视</el-button
      >
      <el-button class="primary" @click="toggleToDefault">默认频道</el-button>
    </div>
  </div>
</template>
<script>
  import PlayerVideo from "./components/PlayerVideo.vue";

  export default {
    components: {
      PlayerVideo,
    },
    data() {
      return {
        volume: 0.5,
        src: "http://vjs.zencdn.net/v/oceans.mp4",
      };
    },
    computed: {
      video() {
        return this.$refs.video;
      },
    },
    methods: {
      // 父类组件调用子组件方法,触发播放器功能
      stopVideo() {
        this.video.stop();
      },
      playVideo() {
        this.video.play();
      },
      reloadVideo() {
        this.video.reload();
      },
      forwardVideo() {
        this.video.forward();
      },
      backVideo() {
        this.video.back();
      },
      fullScreenVideo() {
        this.video.fullScreen();
      },
      screenVideo() {
        this.video.exitScreen();
      },
      volumeUpVideo() {
        this.video.volumeUp();
      },
      volumeDownVideo() {
        this.video.volumeDown();
      },
      toggleToFenghuangwang() {
        this.video.toggleTv({
          src: {
            type: "application/x-mpegURL",
            src: "https://playtv-live.ifeng.com/live/06OLEGEGM4G.m3u8",
          },
          load: "https://playtv-live.ifeng.com/live/06OLEGEGM4G.m3u8",
        });
      },
      toggleToDefault() {
        this.video.toggleTv({
          src: {
            type: "video/mp4",
            src: "http://vjs.zencdn.net/v/oceans.mp4",
          },
          load: "http://vjs.zencdn.net/v/oceans.mp4",
        });
      },
    },
    beforeCreate() {
      this.volume = window.localStorage.volume;
    },
  };
</script>
<style lang="scss">
  #app {
    font-family: Avenir, Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;
    display: flex;
    .video-content {
      display: flex;
      justify-content: center;
      flex-direction: column;
      align-items: center;
      margin-right: 20px;
    }
    .button-group {
      margin-top: 20px;
      display: flex;
      flex: 0 0 100px;
      flex-direction: column;
      justify-content: space-between;
      // align-items: center;
    }
  }
</style>

video-fulfilled

搭建完成。

使用 video.js 搭建视频总结

本教程系统的带大家学习如何使用 video.js 在网站中搭建视频播放器,如果你跟着教程走下来,一定也完成了和教程中一样的视频播放器。如果你觉得太复杂,不想处理前端问题,推荐使用卡拉云,卡拉云是新一代低代码开发工具,内置各类前端组件,无需懂任何前端,仅需拖拽即可快速生成。

下图为使用卡拉云搭建的内部视频审核系统的 Demo 版,仅需拖拽,1小时搞定。

卡拉云 HTML 5 视频播放器

卡拉云是新一代低代码开发工具,免安装部署,可一键接入包括 MySQL 在内的常见数据库及 API。可根据自己的工作流,定制开发。无需繁琐的前端开发,只需要简单拖拽,即可快速搭建企业内部工具。原来三天的开发工作量,使用卡拉云后可缩减至 1 小时,欢迎免费试用卡拉云

扩展阅读:

  • 39
    点赞
  • 224
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: b'vue-video-player'使用教程: 1. 安装vue-video-player 在项目中使用npm或yarn进行安装。 npm install vue-video-player -S 或者 yarn add vue-video-player 2. 引入vue-video-player 在需要使用的组件中引入vue-video-player。 import VueVideoPlayer from 'vue-video-player' import 'video.js/dist/video-js.css' import 'vue-video-player/src/custom-theme.css' Vue.use(VueVideoPlayer) 3. 在模板中使用vue-video-player 在需要播放视频的组件中使用<video-player>标签来引入播放器。 <video-player :options="playerOptions"></video-player> 其中,playerOptions是播放器的配置项,可以自定义配置。 4. 自定义配置 可以通过修改playerOptions来自定义配置。常用的配置项包括autoplay自动播放、controls控制条、sources视频源等。 5. 在vue-video-player中使用插件 vue-video-player支持插件,可以通过插件来扩展其功能,例如全屏、弹幕等。只需要在Vue.use()中添加相应的插件即可。 6. 其他 vue-video-player还支持事件监听、弹幕管理等功能,详细的使用可以参照官方文档。 ### 回答2: Vue-Video-Player是一个开源的基于Vue.js的HTML5视频播放器,在Vue.js基础上,它提供了许多可自定义的 UI 界面和功能。这个组件非常方便,可以很容易地安装、操作和管理。 Vue-Video-Player 的安装非常简便。在终端中输入如下命令: ``` npm install video.js vue-video-player --save ``` 其中,npm 是 Node.js 上的包管理器。这条命令把 video.jsvue-video-player 这两个依赖项安装在你的项目目录下。我们还需要在项目的入口文件 app.js 中引入 vue-video-player 组件和它所依赖的样式文件和插件,具体代码如下: ```javascript import Vue from 'vue'; import VideoPlayer from 'vue-video-player'; import 'video.js/dist/video-js.css'; Vue.use(VideoPlayer); ``` 这样我们就可以在 Vue.js 组件里方便地使用 Vue-Video-Player了。下面是Vue-Video-Player的使用示例: ```javascript <template> <div> <video-player :options="playerOptions"></video-player> </div> </template> <script> export default { name: 'hello', data () { return { playerOptions: { // video.js options controls: true, preload: 'auto', // see more options https://github.com/videojs/video.js/blob/maintutorial-options.html // 引入播放视频的资源 sources: [{ type: 'video/mp4', src: 'https://vjs.zencdn.net/v/oceans.mp4' }], // vue-video-play options fluid: true, height: 'auto', aspectRatio: '16:9' } } } } </script> ``` 其中,我们首先在模板中添加了一个 div 容器,然后在组件中定义了 playerOptions 对象,其中包含了两个部分:视频播放器 Video.js 的配置选项和 Vue-Video-Player 的配置选项。其中 sources 中可以添加多个视频资源,以便在不同的浏览器和设备上进行适配,例如,如果在 Chrome 浏览器上无法播放 MP4 格式的视频,我们可以添加 WebM 格式的视频资源来适配。 最后,我们在 video-player 标签上绑定 options 属性,即可通过 Vue.js 来控制视频播放器的属性,实现自定义的视频播放器功能。 总之,Vue-Video-Player 是一个非常方便的视频播放器组件,它在 Vue.js 的基础上,提供了很多可自定义的 UI 界面和功能,可以在 Web 开发中方便地集成和使用。以上是关于 Vue-Video-Player 的简单介绍和使用示例。 ### 回答3: Vue-Video-Player是一个基于Vuehtml5视频播放器组件库,它可以用于Vue.js应用程序中,能够方便的在页面上嵌入一个可扩展、高效、可定制、易于维护、美观的视频播放器。 1、安装vue-video-player vue-video-player采用ES6语法编写,需要使用babel-loader把源码转换成ES5代码,因此在使用前先要安装相应的工具包,具体步骤如下: 1)安装vue-video-player npm install vue-video-player --save 2)添加VVideoPlayer组件到app.vue // app.vue <template> <v-video-player ref="videoPlayer" :options="playerOptions"> </v-video-player> </template> 3) 在 app.vue 中添加VueVideoPlayer的import语句 <script> import {VueVideoPlayer} from 'vue-video-player' import 'video.js/dist/video-js.css'; export default { name: 'app', components: { 'v-video-player': VueVideoPlayer }, </script> 注意,以上代码必须在广告视频脚本之前提交。 4) 在 main.js 中导入Vue import Vue from 'vue' 5) 在main.js中的Vue实例中添加以下内容: import App from './App.vue' import router from './router' import {VueVideoPlayer} from 'vue-video-player' import 'video.js/dist/video-js.css'; Vue.use(VueVideoPlayer) new Vue({ el: '#app', router, components: { App }, template: '<App/>' }) 到此为止,安装已经成功完成,可以进行后续的使用 2、使用vue-video-player 在Vue.js应用程序中,可以通过使用`VueVideoPlayer`组件来嵌入视频播放器,观看在线视频,具体方法如下: 1)在模板中添加VueVideoPlayer标记: <template> <v-video-player> </v-video-player> </template> 这样,就可以在应用程序页面上嵌入一个视频播放器。 2)通过属性绑定设置视频的一些参数。 v-video-player组件支持很多属性,例如src、options、poster等,这些属性在数据模型中定义好后,就可以通过绑定app.vue来进行使用。 例如下面的代码就设置了视频的标题和封面图片: // app.vue <template> <v-video-player class="video-player" ref="videoPlayer" :options="playerOptions" :src="videoUrl" :poster="posterImage" > <img class="vjs-poster" :src="posterImage" alt="poster"> </v-video-player> </template> export default { name: 'app', data() { return { videoUrl: 'https://vjs.zencdn.net/v/oceans.mp4', playerOptions: { // 视频播放器的一些设置 title: 'Vue Video Player', fluid: true }, posterImage: 'https://vjs.zencdn.net/v/oceans.png' } }, } 3)控制视频播放 控制视频播放,暂停、快进、快退、音量调节以及全屏等操作,可以使用一些API进行实现。 ```javascript // 暂停或播放视频 this.$refs.videoPlayer.toggle() // 停止视频播放 this.$refs.videoPlayer.stop() // 快进5秒,快退5秒 this.$refs.videoPlayer.forward() this.$refs.videoPlayer.backward() // 设置音量(0~1) this.$refs.videoPlayer.setVolume(volume) // 切换全屏 this.$refs.videoPlayer.requestFullscreen() ``` 通过使用以上方法可以轻松实现视频的控制。 总之,vue-video-player是一个非常实用和方便的组件库,它在Vue.js中使用非常简单,能够为我们的项目提供很多有用的功能,可以用来播放多种格式的视频。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值