吉安感知网项目-前端
shuishen
5 days ago 6e88705bd5b443a259b24c17c8a299765d059d96
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
126
127
128
129
130
<script setup>
import { computed, nextTick, onBeforeUnmount, ref, watch } from 'vue'
import videojs from 'video.js'
 
const props = defineProps({
    width: {
        type: String,
        default: '800',
    },
    modelValue: {
        type: Boolean,
        default: false,
    },
    playUrl: {
        type: String,
        default: '',
    },
    title: {
        type: String,
        default: '视频播放',
    },
})
 
const emit = defineEmits(['update:modelValue'])
const videoRefs = ref(null)
let player = null
 
const dialogVisible = computed({
    get: () => props.modelValue,
    set: value => emit('update:modelValue', value),
})
 
function getVideoType(url) {
    const path = (url || '').split('?')[0].toLowerCase()
    if (path.endsWith('.m3u8')) return 'application/x-mpegURL'
    if (path.endsWith('.mp4')) return 'video/mp4'
    if (path.endsWith('.webm')) return 'video/webm'
    if (path.endsWith('.ogg') || path.endsWith('.ogv')) return 'video/ogg'
    if (path.endsWith('.mov')) return 'video/quicktime'
    return ''
}
 
function destroyPlayer() {
    if (!player) return
    player.dispose()
    player = null
}
 
function getSource() {
    const type = getVideoType(props.playUrl)
    return {
        src: props.playUrl,
        ...(type ? { type } : {}),
    }
}
 
async function initPlayer() {
    if (!props.modelValue || !props.playUrl) return
    await nextTick()
    if (!videoRefs.value) return
 
    if (player) {
        player.src(getSource())
        player.load()
        return
    }
 
    player = videojs(videoRefs.value, {
        controls: true,
        preload: 'auto',
        autoplay: false,
        fluid: false,
        width: '100%',
        height: '600px',
        sources: [getSource()],
    })
    player.on('error', () => {
        console.error('视频播放失败:', player?.error(), props.playUrl)
    })
}
 
watch(
    () => props.playUrl,
    () => {
        if (!props.modelValue || !props.playUrl) return
        initPlayer()
    }
)
 
onBeforeUnmount(destroyPlayer)
</script>
 
<template>
    <el-dialog
        class="gd-dialog video-dialog"
        :title="props.title"
        append-to-body
        v-model="dialogVisible"
        width="60%"
        destroy-on-close
        :close-on-click-modal="false"
        @opened="initPlayer"
        @closed="destroyPlayer"
    >
        <video
            ref="videoRefs"
            id="videoPlayDialogPlayer"
            class="video-js vjs-default-skin vjs-big-play-centered"
            controls
            preload="auto"
            playsinline
        ></video>
    </el-dialog>
</template>
 
<style lang="scss">
.video-dialog {
    .video-js {
        height: 600px;
        width: 100%;
    }
}
</style>
 
<style scoped lang="scss">
:deep(.video-js .vjs-tech) {
    height: 100%;
    width: 100%;
}
</style>