一、需求说明
视频自动播放且循环播放前 3 秒
效果如下:
二、实现 Demo
安装 vue-video-player
依赖
yarn add vue-video-player
完整代码:
<template>
<video-player
ref="videoPlayer"
class="video-player-box"
:options="playerOptions"
@timeupdate="onPlayerTimeupdate($event)"
/>
</template>
<script>
import 'video.js/dist/video-js.css'
import { videoPlayer } from 'vue-video-player'
export default {
name: 'Home',
components: { videoPlayer },
data () {
return {
playerOptions: {
width: 300,
height: 500,
muted: true,
autoplay: true,
controls: false,
sources: [{
type: 'video/mp4',
src: 'http://qiuzi-resource.oss-cn-shenzhen.aliyuncs.com/video.MP4'
}]
}
}
},
methods: {
onPlayerTimeupdate (player) {
const currentTime = Number.parseInt(player.currentTime())
// 循环播放前 3 秒
if (currentTime === 3) {
player.currentTime(0)
}
}
}
}
</script>
<style lang="scss">
.video-player-box {
video {
object-fit: cover;
}
}
</style>