- 安装插件
npm i videojs
npm install --save video.jsnpm install --save video.js //窗口样式
- 局部引入
import videojs from 'video.js'
import 'video.js/dist/video-js.css'
import 'videojs-contrib-hls'
- 使用
<video id="videoPlayer" class="video-js"></video>
// 配置
data: {
return {
options: {
autoplay: true, // 设置自动播放
muted: true, // 设置了它为true,才可实现自动播放,同时视频也被静音(Chrome66及以上版本,禁止音视频的自动播放)
preload: 'auto', // 预加载
controls: true // 显示播放的控件
},
player: null,
}
}
// methods
methods: {
getVideo (nowPlayVideoUrl, index) {
this.selectIndex = index
this.player = videojs('videoPlayer', this.options)
this.player.src([
{
src: nowPlayVideoUrl.hlsStandard, // 地址
type: 'application/x-mpegURL' // 告诉videojs,这是一个hls流
}
])
},
}
// 销毁
beforeDestroy () {
if (this.player) {
this.player.dispose()
}
}
// 官方例子
<template>
<div>
<video ref="videoPlayer" class="video-js"></video>
</div>
</template>
<script>
import videojs from 'video.js';
export default {
name: "VideoPlayer",
props: {
options: {
type: Object,
default() {
return {};
}
}
},
data() {
return {
player: null
}
},
mounted() {
this.player = videojs(this.$refs.videoPlayer, this.options, function onPlayerReady() {
console.log('onPlayerReady', this);
})
},
beforeDestroy() {
if (this.player) {
this.player.dispose()
}
}
}
</script>