首先需要安装依赖包
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上的文档,点击查看