VideoPlay.vue
2.05 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
<script lang="ts" setup>
import { isNumber } from 'lodash';
import videoJs, { VideoJsPlayer, VideoJsPlayerOptions } from 'video.js';
import 'video.js/dist/video-js.css';
import { computed, CSSProperties, onMounted, onUnmounted, ref, unref } from 'vue';
import { useDesign } from '/@/hooks/web/useDesign';
const { prefixCls } = useDesign('basic-video-play');
const props = defineProps<{
options?: VideoJsPlayerOptions;
}>();
const emit = defineEmits<{
(event: 'ready', instance?: Nullable<VideoJsPlayer>): void;
}>();
const videoPlayEl = ref<HTMLVideoElement>();
const videoPlayInstance = ref<Nullable<VideoJsPlayer>>();
const getOptions = computed(() => {
const { options } = props;
const defaultOptions: VideoJsPlayerOptions = {
language: 'zh',
muted: true,
liveui: true,
controls: true,
};
return { ...defaultOptions, ...options };
});
const getWidthHeight = computed(() => {
let { width = 300, height = 150 } = unref(getOptions);
width = isNumber(width) ? (`${width}px` as unknown as number) : width;
height = isNumber(height) ? (`${height}px` as unknown as number) : height;
return { width, height } as CSSProperties;
});
const init = () => {
videoPlayInstance.value = videoJs(unref(videoPlayEl)!, unref(getOptions), () => {
emit('ready', unref(videoPlayInstance));
});
};
onMounted(() => {
init();
});
onUnmounted(() => {
unref(videoPlayInstance)?.dispose();
videoPlayInstance.value = null;
});
</script>
<template>
<div :class="prefixCls" class="w-full h-full" :style="getWidthHeight">
<video
ref="videoPlayEl"
class="video-js vjs-big-play-centered vjs-show-big-play-button-on-pause !w-full !h-full"
></video>
</div>
</template>
<style lang="less">
@prefix-cls: ~'@{namespace}-basic-video-play';
.@{prefix-cls} {
.vjs-error-display {
.vjs-modal-dialog-content::after {
content: '无法加载视频,原因可能是服务器或网络故障,也可能是格式不支持.';
}
}
}
</style>