index.vue
1.81 KB
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
<template>
<div class="go-content-box" :style="{ width: w + 'px', height: h + 'px' }">
<video id="my-player" ref="videoRef" class="video-js my-video">
<source :src="props.chartConfig.option.dataset" />
</video>
</div>
</template>
<script setup lang="ts">
import { PropType, onMounted, ref, watch, toRefs } from 'vue'
import { CreateComponentType } from '@/packages/index.d'
import videojs from 'video.js'
import type { VideoJsPlayerOptions } from 'video.js'
import 'video.js/dist/video-js.min.css'
const props = defineProps({
chartConfig: {
type: Object as PropType<CreateComponentType>,
required: true
}
})
const { w, h } = toRefs(props.chartConfig.attr)
// video标签
const videoRef = ref<HTMLElement | null>(null)
// video实例对象
let videoPlayer: videojs.Player | null = null
// 初始化videojs
const initVideo = () => {
const options: VideoJsPlayerOptions = {
language: 'zh-CN', // 设置语言
controls: true, // 是否显示控制条
preload: 'auto', // 预加载
autoplay: true, // 是否自动播放
fluid: false, // 自适应宽高
src: props.chartConfig.option.dataset, // 要嵌入的视频源的源 URL
userActions: {
hotkeys: true
}
}
if (videoRef.value) {
// 创建 video 实例
videoPlayer = videojs(videoRef.value, options, onPlayerReady)
}
}
// video初始化完成的回调函数
const onPlayerReady = () => {}
onMounted(() => {
initVideo()
})
watch(
() => props.chartConfig.option.dataset,
newData => {
console.log(newData)
props.chartConfig.option.dataset = newData
initVideo()
}
)
</script>
<style lang="scss" scoped>
.go-content-box {
display: flex;
align-items: center;
justify-content: center;
.my-video {
width: 100% !important;
height: 100% !important;
position: relative;
}
}
</style>