web
3 天以前 e630e68f1931a4891381e4b1c7be4b4b02c74ff8
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<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>
        </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 isPlay = ref(false);
const showPlay = ref(false);
const showPause = ref(false);
let timer = null;
let timerOut = null;
 
//通知设备上传媒体流,做节流,防止过度点击
const handlePlayer = debounce(function() {
    showPlay.value = false;
    // 获取播放地址
    getVideoMedia(props.item.deviceId, props.item.channelId).then(res => {
        videojsb.value.play(res.data.url);
        isPlay.value = true;
        showPlay.value = false;
    }).catch(() => {
        isPlay.value = false;
        showPlay.value = true;
    })
    // 挂定时任务,2分钟获取一次直播流数据,防止客户关闭页面,如果2分钟未获取,后端断开链接
    if(timer) clearInterval(timer)
    timer = setInterval(() => {
        // 调用续时点播
        renewVideoMedia(props.item.deviceId, props.item.channelId)
    }, 120000)
}, 2000, true)
 
const hanlePause = () => {
    isPlay.value = false;
    showPause.value = false;
    showPlay.value = true;
    clearInterval(timer)
    clearTimeout(timerOut);
    stopVideoMedia(props.item.deviceId, props.item.channelId).then(() => {
        console.log('关闭成功')
        videojsb.value.pause()
    })
}
 
const handleEnter = () => {
    if(props.item.deviceId && props.item.channelId) {
        if(isPlay.value) {
            if(timerOut) clearTimeout(timerOut);
            showPause.value = true;
            // 延时2秒隐藏暂停按钮
            timerOut = setTimeout(() => {
                showPause.value = false;
            }, 3000)
        } else {
            showPlay.value = true;
        }
    }
}
 
const handleLeave = () => {
    if(timerOut) clearTimeout(timerOut);
    showPause.value = false;
    showPlay.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: false,
                play: false,
                audio: true,
                recorder: false,
            },
            controlAutoHide: false,
            forceNoOffscreen: false,
            hasAudio: false,
            heartTimeout: 5,
            heartTimeoutReplay:true,
            heartTimeoutReplayTimes: 3,
            hiddenAutoPause: false,
            isNotMute: false,
            hotKey: true,
            isFlv: false,
            recordType: "mp4",
        })
    );
}
 
// 初始化
const init = async () => {
    await initDom()
    // 自动播放
    if(props.autoPlay && props.item.deviceId && props.item.channelId) {
        handlePlayer()
    }
    if(!props.item.deviceId || !props.item.channelId) {
        showPlay.value = false;
    }
};
 
onMounted(() => {
    init();
});
 
onUnmounted(() => {
    if(videojsb.value) {
        videojsb.value.destroy();
    }
    if(timer) clearInterval(timer)
    if(timerOut) clearTimeout(timerOut)
})
 
 
// 外抛方法
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;
        }
    }
    .mask{
        position: absolute;
    }
}
</style>