VideoPlay.vue
3.44 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<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, toRaw, unref } from 'vue';
import { useDesign } from '/@/hooks/web/useDesign';
import { getJwtToken, getShareJwtToken } from '/@/utils/auth';
import { isShareMode } from '/@/views/sys/share/hook';
import 'videojs-flvjs-es6';
const { prefixCls } = useDesign('basic-video-play');
const props = withDefaults(
defineProps<{
options?: VideoJsPlayerOptions;
withToken?: boolean;
immediateInitOnMounted?: boolean;
}>(),
{
immediateInitOnMounted: true,
}
);
const emit = defineEmits<{
(event: 'ready', instance?: Nullable<VideoJsPlayer>): void;
(event: 'onUnmounted'): void;
}>();
const videoPlayEl = ref<HTMLVideoElement>();
const videoPlayInstance = ref<Nullable<VideoJsPlayer>>();
const getOptions = computed(() => {
const { options, withToken } = props;
const defaultOptions: VideoJsPlayerOptions & Recordable = {
language: 'zh',
muted: true,
liveui: true,
controls: true,
techOrder: ['html5', 'flvjs'],
flvjs: {
mediaDataSource: {
isLive: true,
cors: true,
hasAudio: false,
withCredentials: false,
},
config: {
headers: {
...(withToken
? {
'X-Authorization': `Bearer ${isShareMode() ? getShareJwtToken() : getJwtToken()}`,
}
: {}),
},
autoCleanupSourceBuffer: true,
},
},
};
return videoJs.mergeOptions(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 = () => {
if (unref(videoPlayInstance)) unref(videoPlayInstance)?.dispose();
videoPlayInstance.value = videoJs(unref(videoPlayEl)!, unref(getOptions), () => {
emit('ready', unref(videoPlayInstance));
});
};
const customInit = (getOptionsFn: (optios: VideoJsPlayerOptions) => VideoJsPlayerOptions) => {
return (videoPlayInstance.value = videoJs(
unref(videoPlayEl)!,
getOptionsFn(toRaw(unref(getOptions))),
() => {
emit('ready', unref(videoPlayInstance));
}
));
};
onMounted(() => {
props.immediateInitOnMounted && init();
});
onUnmounted(() => {
unref(videoPlayInstance)?.dispose();
videoPlayInstance.value = null;
emit('onUnmounted');
});
defineExpose({
customInit,
reloadPlayer: init,
getInstance: () => unref(videoPlayInstance),
});
</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"
muted
>
</video>
</div>
</template>
<style lang="less">
@prefix-cls: ~'@{namespace}-basic-video-play';
.@{prefix-cls} {
.vjs-error-display {
.vjs-modal-dialog-content::after {
content: '无法加载视频,原因可能是服务器或网络故障,也可能是格式不支持.';
}
}
}
</style>