VUE2项目中 引入vue-video-player
- 安装依赖
npm install vue-video-player@5.0.1 --save
- main.js引入
require('vue-video-player/src/custom-theme.css')
require('video.js/dist/video-js.css')
import VideoPlayer from 'vue-video-player'
Vue.use(VideoPlayer)
- 引入组件
<template>
<div class="player">
<video-player class="video-player vjs-custom-skin"
ref="videoPlayer"
:playsinline="true"
:options="playerOptions"
@play="onPlayerPlay($event)"
@pause="onPlayerPause($event)"
@timeupdate="onPlayerTimeupdate($event)"
@ready="playerReadied"
@ended="onPlayerEnded($event)"
>
</video-player>
</div>
</template>
<script>
import { videoPlayer } from 'vue-video-player';
export default {
name: "VideoPlayer",
components: {
videoPlayer
},
data() {
return {
playerOptions: {
playbackRates: [0.5, 1.0, 1.5, 2.0], //播放速度
autoplay: false, //如果true,浏览器准备好时开始回放。
muted: false, // 默认情况下将会消除任何音频。
loop: false, // 导致视频一结束就重新开始。
preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频(如果浏览器支持)
language: 'zh-CN',
aspectRatio: '16:9', // 将播放器置于流畅模式,并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字(例如"16:9"或"4:3")
fluid: true, // 当true时,Video.js player将拥有流体大小。换句话说,它将按比例缩放以适应其容器。
sources: [{
type: "video/mp4",
src: "" //你的视频地址(必填)
}],
poster: "", //你的封面地址
width: document.documentElement.clientWidth,
notSupportedMessage: '此视频暂无法播放,请稍后再试',
controlBar: {
timeDivider: true,
durationDisplay: true,
remainingTimeDisplay: false,
fullscreenToggle: true
}
},
gk: ""
}
},
methods: {
onPlayerPlay(player) {
// 播放回调
},
onPlayerPause(player){
// 暂停回调
},
onPlayerEnded($event) {
//播放完成回调
},
goFast() {
//快进
this.gk += 5;
this.$refs.videoPlayer.player.currentTime(this.gk)
},
goBack() {
//后退
this.gk -= 5;
this.$refs.videoPlayer.player.currentTime(this.gk);
},
onPlayerTimeupdate(player) {
//获取当前播放时间
this.gk = player.cache_.currentTime;
},
playerReadied(player) {
player.currentTime(this.gk);
},
},
computed: {
player() {
return this.$refs.videoPlayer.player
}
},
watch:{
gk:function(){
}
},
mounted() {
let that = this
document.addEventListener("keydown",function(e){
if (e.keyCode === 39) {
that.goFast()
}
if (e.keyCode === 37) {
that.goBack()
}
})
},
}
</script>
<style scoped>
.vjs-custom-skin{
}
</style>
- 改为圆形播放按钮的一种解决方式
新建一个video.css文件
.video-js .vjs-big-play-button{
/*
播放按钮换成圆形
*/
height: 2em;
width: 2em;
line-height: 2em;
border-radius: 1em;
}
引入样式
require('@/css/video.css')