video.js和Vue集成
- 首先在 main.js 中导入、注册 video.js
javascript
import videojs from 'video.js'
import 'video.js/dist/video-js.min.css'
import video_zh_CN from 'video.js/dist/lang/zh-CN.json'
import video_en from 'video.js/dist/lang/en.json'
videojs.addLanguage('zh_cn', video_zh_CN) // 键名必须是zh_cn,不是将影响国际化
videojs.addLanguage('en', video_en)
Vue.prototype.$video = videojs
- 在 mounted 上实例化 video.js 播放器,并在 beforeDestroy 上销毁它
html
<template>
<video ref="videoPlayer"
class="video-js vjs-default-skin"
width="412"
controls>
<source :src="src" />
</video>
</template>
<script>
import videojs from 'video.js';
export default {
name: "VideoPlayer",
props: {
options: {
type: Object,
default() {
return {};
}
},
src: {
type: String
}
},
data() {
return {
player: null
}
},
mounted() {
this.player = this.$video(this.$refs.videoPlayer, this.options, function onPlayerReady()
{
console.log('onPlayerReady', this)
console.log(this.language()) // this.language()方法可以打印当前Video.js实例的语种键值
})
},
beforeDestroy() {
if (this.player) {
this.player.dispose()
}
}
}
</script>
- 然后你可以这样使用它:
html
<template>
<div>
<!-- 国际化时区分传参即可,例如 zh_cn | en 等 -->
<!-- 如果需要简体中文,必须传 zh_cn -->
<video-player :options="{ language: 'zh_cn' }" src="某某mp4的url"/>
</div>
</template>
<script>
import VideoPlayer from "@/components/VideoPlayer.vue";
export default {
name: "VideoExample",
components: {
VideoPlayer
}
}
</script>