web
13 小时以前 d20fcd6ad28a534d763273c48381cdc36ff2891f
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<template>
    <div class="videoBox" @mouseenter="handleEnter" @mouseleave="handleLeave">
        <div class="video" ref="videoRef" style="width: 100%; height: 100%"></div>
        <div class="playerBtn">
            <el-icon class="icon" v-if="showPlay" @click="handlePlayer"><VideoPlay /></el-icon>
            <el-icon class="icon" v-if="showPause" @click="hanlePause"><VideoPause /></el-icon>
            <el-icon class="icon is-loading" v-if="showLoading"><Loading /></el-icon>
        </div>
    </div>
</template>
 
<script setup lang="ts">
import {onMounted, onUnmounted, ref} from 'vue';
import { getVideoMedia, stopVideoMedia, renewVideoMedia } from '@/api/screen/video';
import { debounce } from '@/utils/tool';
 
const props = defineProps({
    item: {
      type: Object,
      default: {}
    },
    autoPlay: {
        type: Boolean,
        default: false
    }
});
 
const videoRef = ref(null);
const videojsb = ref();
const showPlay = ref(true);
const showPause = ref(false);
const showLoading = ref(false);
let timer = null;
 
//通知设备上传媒体流,做节流,防止过度点击
const handlePlayer = debounce(function() {
    showPlay.value = false;
    showLoading.value = true;
    // 获取播放地址
    getVideoMedia(props.item.deviceId, props.item.channelId).then(res => {
        videojsb.value.play(res.data.url);
    }).catch(() => {
        showPlay.value = true;
    }).finally(() => {
        showLoading.value = false;
    })
    // 挂定时任务,2分钟获取一次直播流数据,防止客户关闭页面,如果2分钟未获取,后端断开链接
    if(timer) clearInterval(timer)
    timer = setInterval(() => {
        // 调用续时点播
        renewVideoMedia(props.item.deviceId, props.item.channelId)
    }, 120000)
}, 2000, true)
 
const hanlePause = () => {
    showPause.value = false;
    showPlay.value = true;
    clearInterval(timer)
    stopVideoMedia(props.item.deviceId, props.item.channelId).then(() => {
        console.log('关闭成功')
    })
}
 
const handleEnter = () => {
    if(!showPlay.value && !showLoading.value) {
        showPause.value = true;
    }
}
 
const handleLeave = () => {
    showPause.value = false;
}
 
// 实例化dom节点
const initDom = async () => {
    videojsb.value = new window.Jessibuca(
        Object.assign({
            container: videoRef.value,
            isResize: true,
            isFullResize: true,
            text: '',
            videoBuffer: 2,
            loadingText: '加载中...',
            useMSE: true,
            useWCS: true,
            autoWasm: true,
            // debug: true,
            supportDblclickFullscreen: false,
            operateBtns: {
                fullscreen: true,
                play: false,
                audio: true,
                recorder: false,
            },
            controlAutoHide: true,
            forceNoOffscreen: true,
            isNotMute: false,
            heartTimeoutReplay:false,
        })
    );
}
 
// 初始化
const init = async () => {
    await initDom()
    // 自动播放
    if(props.autoPlay && props.item.deviceId && props.item.channelId) {
        handlePlayer()
    }
};
 
onMounted(() => {
    init();
});
 
onUnmounted(() => {
    if(videojsb.value) {
        videojsb.value.destroy();
    }
    if(timer) clearInterval(timer)
})
 
 
// 外抛方法
defineExpose({
    handlePlayer
})
</script>
 
<style scoped lang="scss">
.videoBox {
    width: 100%;
    height: 100%;
    position: relative;
    .video {
        width: 100%;
        height: 100%;
        background-color: #000;
    }
    .playerBtn {
        width: 4rem;
        height: 4rem;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        z-index: 100;
        .icon {
            color: #fff;
            font-size: 3rem;
        }
    }
    :deep(.jessibuca-controls) {
        background-color: transparent;
    }
}
</style>