zhuguifei
2025-04-28 442928123f63ee497d766f9a7a14f0a6ee067e25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<template>
    <video ref="videoPlayer" class="video-js"></video>
</template>
 
<script>
import videojs from 'video.js'
import 'video.js/dist/video-js.css'
 
export default {
    name: 'VideoPlayer',
    props: {
        source: {
            type: Object,
            default: () => {
                return {}
            }
        }
    },
    data() {
        return {
            // video.js 播放器的配置项,具体查看 https://videojs.com/getting-started
            options: {
                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: [this.source], //  视频源信息
                notSupportedMessage: '此视频暂无法播放,请稍后再试',
                controls: true,
                controlBar: {
                    timeDivider: true,
                    durationDisplay: true,
                    remainingTimeDisplay: false,
                    fullscreenToggle: true // 全屏按钮
                }
            },
            player: null
        }
    },
    watch: {
        /**
         * 监听视频源信息变化
         * @param {object} newValue 变化后的值
         */
        source(newValue) {
            this.player.pause() //  暂停
            this.player.currentTime(0) //  跳到 0 秒
            this.player.src(newValue) //  动态设置视频源
            this.player.play() //  加载完毕后自动播放
        }
    },
    mounted() {
        this.player = videojs(this.$refs.videoPlayer, this.options)
        window.videojs = videojs
        require('video.js/dist/lang/zh-CN.js')
    },
    beforeDestroy() {
        if (this.player) {
            this.player.dispose()
        }
    }
}
</script>
 
<style lang="less" scoped>
@import '~@assets/file/less/varibles.less';
 
 
.video-js {
  width: 100%;
  height: 100%;
  padding: 0;
  color: @Primary;
 
  // 播放按钮
  /deep/ .vjs-big-play-button {
    border-radius: 50%;
    border: 6px solid @Primary;
    left: calc(50% - 1em);
    top: calc(50% - 1em);
    width: 2.5em;
    height: 2.5em;
    line-height: 2.5em;
    background: transparent;
    box-sizing: content-box;
    .vjs-icon-placeholder:before {
      font-size: 48px;
    }
    &:hover {
      opacity: 0.6;
    }
  }
  // 改变各种“条”的颜色
  /deep/ .vjs-volume-level,
  /deep/ .vjs-play-progress,
  /deep/ .vjs-slider-bar {
    background: @Primary;
  }
 
  // 控制条按钮的大小
  /deep/ .vjs-control-bar {
    font-size: 14px;
  }
 
}
</style>