Nuxt.js中使用video.js

首先需要安装依赖包

npm install vue-video-player --save

然后在plugins目录下新建文件video-player.js

video-player.js里写下如下代码

import Vue from 'vue'

const VueVideoPlayer = require('vue-video-player/dist/ssr')
Vue.use(VueVideoPlayer)

然后打开nuxt.config.js,写下如下代码,分别引入css和js

module.exports = {
  // some nuxt config...
  plugins: [
    { src: '~plugins/nuxt-video-player-plugin.js', ssr: false }
  ],
  // some nuxt config...
  css: [
    'video.js/dist/video-js.css'
  ],
  // some nuxt config...
}

在组件中使用

<template>
  <section class="container">
    <div class="video-player-box"
         :playsinline="playsinline"
         @play="onPlayerPlay($event)"
         @pause="onPlayerPause($event)"
         @ended="onPlayerEnded($event)"
         @loadeddata="onPlayerLoadeddata($event)"
         @waiting="onPlayerWaiting($event)"
         @playing="onPlayerPlaying($event)"
         @timeupdate="onPlayerTimeupdate($event)"
         @canplay="onPlayerCanplay($event)"
         @canplaythrough="onPlayerCanplaythrough($event)"
         @ready="playerReadied"
         @statechanged="playerStateChanged($event)"
         v-video-player:myVideoPlayer="playerOptions">
    </div>
  </section>
</template>

<script>
  export default {
    data () {
      return {
        playsinline: true,
        playerOptions: {//播放器配置
          muted: false,//是否静音
          language: 'en',
          width:'900px',
          height:'500px',
          playbackRates: [0.7, 1.0, 1.5, 2.0],//播放速度
          sources: [{
            type: "video/mp4",
            src: "https://cdn.theguardian.tv/webM/2015/07/20/150716YesMen_synd_768k_vp8.webm"
          }],
          poster: "",//封面图
        }
      }
    },
    mounted() {
      console.log('this is current player instance object', this.myVideoPlayer)
    },
    methods: {
      // 监听播放
      onPlayerPlay(player) {
        // console.log('player play!', player)
      },
      // 监听暂停
      onPlayerPause(player) {
        // console.log('player pause!', player)
      },
      // 监听停止
      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) {
        // console.log('player Timeupdate!', player.currentTime())
      },
      onPlayerCanplay(player) {
        console.log('player Canplay!', player)
      },
      onPlayerCanplaythrough(player) {
        // console.log('player Canplaythrough!', player)
      },
      // 监听状态改变
      playerStateChanged(playerCurrentState) {
        // console.log('player current update state', playerCurrentState)
      },
      // 监听播放器准备就绪
      playerReadied(player) {
        // console.log('example 01: the player is readied', player)
      }
    }
  }
</script>

<style lang="scss" scoped>
</style>

最后贴上在npm上的文档,点击查看

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端薛小帅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值