EasyPlayer.vue
13.7 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<template>
<div
class="player-wrapper"
:class="[playerClassOptions, { 'force-hide-controls': !showControls }]"
@click="onPlayerClick"
@mousemove="onMouseMove"
@mouseleave="onMouseLeave"
>
<div class="custom-top-bar" :class="{ 'hide-bar': !showControls }">
<div class="top-bar-left">
<span class="video-title">{{ videoTitle || '实时监控' }}</span>
</div>
<div class="top-bar-right">
<span class="net-speed">
<i class="el-icon-odometer"></i> {{ netSpeed }}
</span>
</div>
</div>
<div :id="uniqueId" :key="uniqueId" ref="container" class="player-box"></div>
<div v-show="!hasUrl" class="idle-mask">
<div class="idle-text">无信号</div>
</div>
<div v-show="hasUrl && isLoading && !isError" class="status-mask loading-mask">
<div class="spinner-box">
<div class="simple-spinner"></div>
</div>
<div class="status-text">视频连接中...</div>
</div>
<div v-if="hasUrl && isError" class="status-mask error-mask">
<div class="error-content">
<i class="el-icon-warning-outline" style="font-size: 30px; color: #ff6d6d; margin-bottom: 10px;"></i>
<div class="status-text error-text">{{ errorMessage }}</div>
<el-button type="primary" size="mini" icon="el-icon-refresh-right" @click.stop="handleRetry" style="margin-top: 10px;">重试</el-button>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'VideoPlayer',
props: {
initialPlayUrl: { type: [String, Object], default: '' },
videoTitle: { type: String, default: '' },
isResize: { type: Boolean, default: true },
hasAudio: { type: Boolean, default: true },
loadTimeout: { type: Number, default: 20000 },
showCustomMask: { type: Boolean, default: true }
},
data() {
return {
// 初始 ID
uniqueId: `player-box-${Date.now()}-${Math.floor(Math.random() * 1000)}`,
playerInstance: null,
// 状态
isLoading: false,
isError: false,
errorMessage: '',
hasStarted: false,
// 交互
netSpeed: '0KB/s',
showControls: false,
controlTimer: null,
retryCount: 0,
controlsConfig: {
showBottomBar: true,
showSpeed: false,
showCodeSelect: false,
showPlay: true,
showAudio: true,
showStretch: true,
showScreenshot: true,
showRecord: true,
showZoom: true,
showFullscreen: true,
}
};
},
computed: {
hasUrl() {
const url = this.initialPlayUrl;
if (!url) return false;
if (typeof url === 'string') return url.length > 0;
return !!url.videoUrl;
},
playerClassOptions() {
const c = this.controlsConfig;
return {
'hide-bottom-bar': !c.showBottomBar,
'hide-speed': !c.showSpeed,
'hide-code-select': !c.showCodeSelect,
'hide-btn-play': !c.showPlay,
'hide-btn-audio': !c.showAudio,
'hide-btn-stretch': !c.showStretch,
'hide-btn-screenshot': !c.showScreenshot,
'hide-btn-record': !c.showRecord,
'hide-btn-zoom': !c.showZoom,
'hide-btn-fullscreen': !c.showFullscreen,
};
}
},
watch: {
initialPlayUrl: {
handler(newUrl) {
const url = typeof newUrl === 'string' ? newUrl : (newUrl && newUrl.videoUrl) || '';
if (url) {
// 有地址:准备播放
this.isLoading = true;
this.isError = false;
if (this.playerInstance) {
// 实例已存在(比如轮播切换下一路),直接切换地址,不要销毁
this.play(url);
} else {
// 实例不存在(比如从全部关闭状态恢复),创建并播放
this.$nextTick(() => {
this.create();
setTimeout(() => this.play(url), 100);
});
}
} else {
// 地址为空(全部关闭):彻底销毁
this.destroy();
this.isLoading = false;
this.isError = false;
}
},
immediate: true
},
hasAudio() {
if (this.hasUrl && this.playerInstance) {
this.destroyAndReplay(this.initialPlayUrl);
}
}
},
beforeDestroy() {
this.destroy();
if (this.controlTimer) clearTimeout(this.controlTimer);
},
methods: {
onMouseMove() {
if (!this.hasStarted) return;
this.showControls = true;
if (this.controlTimer) clearTimeout(this.controlTimer);
this.controlTimer = setTimeout(() => {
this.showControls = false;
}, 3000);
},
onMouseLeave() {
this.showControls = false;
},
onPlayerClick() {
this.$emit('click');
},
create() {
if (this.playerInstance) return;
const container = this.$refs.container;
// 容错:如果容器不存在,或者刚才被销毁还没渲染出来,延迟重试
if (!container || container.clientWidth === 0) {
if (this.retryCount < 10) { // 增加重试次数
this.retryCount++;
setTimeout(() => this.create(), 100);
}
return;
}
// 再次确保清空内容
container.innerHTML = '';
if (!window.EasyPlayerPro) return;
try {
const c = this.controlsConfig;
this.playerInstance = new window.EasyPlayerPro(container, {
bufferTime: 0.2,
stretch: !this.isResize,
MSE: true,
WCS: true,
hasAudio: this.hasAudio,
isLive: true,
loading: false, // 使用我们的自定义 loading
isBand: true,
btns: {
play: c.showPlay,
audio: c.showAudio,
fullscreen: c.showFullscreen,
screenshot: c.showScreenshot,
record: c.showRecord,
stretch: c.showStretch,
zoom: c.showZoom,
}
});
this.retryCount = 0;
this.playerInstance.on('kBps', (speed) => {
this.netSpeed = speed + '/S';
});
this.playerInstance.on('play', () => {
console.log(`[${this.uniqueId}] 播放成功`);
this.hasStarted = true;
this.isLoading = false;
this.isError = false;
// 播放成功后显示一下控制栏
this.showControls = true;
if (this.controlTimer) clearTimeout(this.controlTimer);
this.controlTimer = setTimeout(() => {
this.showControls = false;
}, 3000);
});
this.playerInstance.on('error', (err) => {
console.error('Player Error:', err);
this.triggerError('视频流连接异常');
});
} catch (e) {
console.error("Create Instance Error:", e);
// 如果创建报错,可能是容器脏了,执行销毁逻辑刷新DOM
this.destroy();
}
},
play(url) {
if (!url) return;
if (!this.playerInstance) {
this.create();
setTimeout(() => this.play(url), 200);
return;
}
this.isLoading = true;
this.isError = false;
this.errorMessage = '';
// 设置超时检测
setTimeout(() => {
if (this.isLoading) this.triggerError('连接超时,请重试');
}, this.loadTimeout);
this.playerInstance.play(url).catch(e => {
console.warn("Play error:", e);
this.triggerError('请求播放失败');
});
},
// 【核心修复】彻底销毁并刷新DOM ID
destroy() {
// 1. 重置状态
this.hasStarted = false;
this.showControls = false;
this.netSpeed = '0KB/s';
this.isLoading = false;
this.isError = false;
const container = this.$refs.container;
// 2. 销毁实例
if (this.playerInstance) {
try {
this.playerInstance.destroy();
} catch(e) {}
this.playerInstance = null;
}
// 3. 暴力清理旧 DOM
if (container) {
container.innerHTML = '';
}
// 4. 【关键】更新 uniqueId
// 这会强制 Vue 在下一次渲染时移除当前的 div,并创建一个全新的 div
// 从而彻底解决 "EasyPlayerPro err container" 错误
this.uniqueId = `player-box-${Date.now()}-${Math.floor(Math.random() * 1000)}`;
},
destroyAndReplay(url) {
this.destroy();
this.isLoading = true;
this.$nextTick(() => {
this.create();
if (url) {
const u = typeof url === 'string' ? url : url.videoUrl;
this.play(u);
}
});
},
handleRetry() {
this.destroyAndReplay(this.initialPlayUrl);
},
triggerError(msg) {
if (this.hasUrl) {
this.isLoading = false;
this.isError = true;
this.errorMessage = msg;
}
},
setControls(config) {
this.controlsConfig = { ...this.controlsConfig, ...config };
}
}
};
</script>
<style scoped>
/* 基础布局 */
.player-wrapper {
width: 100%; height: 100%; display: flex; flex-direction: column;
position: relative; background: #000; overflow: hidden;
}
.player-box {
flex: 1; width: 100%; height: 100%; background: #000; position: relative; z-index: 1;
}
/* 顶部栏 */
.custom-top-bar {
position: absolute; top: 0; left: 0; width: 100%; height: 40px;
background: linear-gradient(to bottom, rgba(0,0,0,0.8), rgba(0,0,0,0));
z-index: 200; display: flex; justify-content: space-between; align-items: center;
padding: 0 15px; box-sizing: border-box; pointer-events: none; transition: opacity 0.3s ease;
}
.custom-top-bar.hide-bar { opacity: 0; }
.top-bar-left .video-title { color: #fff; font-size: 14px; font-weight: bold; }
.top-bar-right .net-speed { color: #00ff00; font-size: 12px; font-family: monospace; }
/* 无信号遮罩
z-index 必须高于 player-box (z-index 1)
并且背景设为纯黑,以盖住可能残留的画面
*/
.idle-mask {
position: absolute; top: 0; left: 0; width: 100%; height: 100%;
background-color: #000; z-index: 10;
display: flex; align-items: center; justify-content: center;
}
.idle-text { color: #555; font-size: 14px; }
/* 状态蒙层 */
.status-mask {
position: absolute; top: 0; left: 0; width: 100%; height: 100%;
background-color: #000; z-index: 50;
display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.status-text { color: #fff; margin-top: 15px; font-size: 14px; letter-spacing: 1px; }
.error-content { display: flex; flex-direction: column; align-items: center; justify-content: center; }
.error-text { color: #ff6d6d; }
/* Loading */
.spinner-box { width: 50px; height: 50px; display: flex; justify-content: center; align-items: center; }
.simple-spinner {
width: 40px; height: 40px; border: 3px solid rgba(255, 255, 255, 0.2);
border-radius: 50%; border-top-color: #409EFF; animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
</style>
<style>
.player-wrapper.hide-speed .easyplayer-speed { display: none !important; }
.player-wrapper.hide-code-select .easyplayer-controls-code-wrap { display: none !important; }
.player-wrapper.hide-bottom-bar .easyplayer-controls { display: none !important; }
.player-wrapper.hide-btn-play .easyplayer-play, .player-wrapper.hide-btn-play .easyplayer-pause { display: none !important; }
.player-wrapper.hide-btn-audio .easyplayer-audio-box { display: none !important; }
.player-wrapper.hide-btn-screenshot .easyplayer-screenshot { display: none !important; }
.player-wrapper.hide-btn-fullscreen .easyplayer-fullscreen, .player-wrapper.hide-btn-fullscreen .easyplayer-fullscreen-exit { display: none !important; }
.player-wrapper.hide-btn-record .easyplayer-record, .player-wrapper.hide-btn-record .easyplayer-record-stop { display: none !important; }
.player-wrapper .easyplayer-recording {
display: none; position: absolute !important; top: 50px !important; left: 50% !important;
transform: translateX(-50%) !important; z-index: 200 !important;
background: rgba(255, 0, 0, 0.6); border-radius: 4px; padding: 4px 12px;
}
.player-wrapper .easyplayer-recording[style*="block"] { display: flex !important; align-items: center !important; justify-content: center !important; }
.player-wrapper .easyplayer-recording-time { margin: 0 8px; font-size: 14px; color: #fff; }
.player-wrapper .easyplayer-recording-stop { height: auto !important; cursor: pointer; }
.player-wrapper.hide-btn-stretch .easyplayer-stretch { display: none !important; }
.player-wrapper .easyplayer-stretch { font-size: 0 !important; width: 34px !important; height: 100% !important; display: flex !important; align-items: center; justify-content: center; cursor: pointer; }
.player-wrapper .easyplayer-stretch::after {
content: ''; display: block; width: 20px; height: 20px;
background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg viewBox='0 0 1024 1024' xmlns='http://www.w3.org/2000/svg' width='200' height='200'%3E%3Cpath d='M128 128h298.667v85.333H195.2l201.6 201.6-60.373 60.374-201.6-201.6v231.466H49.067V49.067h78.933V128zM896 896H597.333v-85.333H828.8l-201.6-201.6 60.373-60.374 201.6 201.6V518.933h85.334v377.067h-78.934V896z' fill='%23ffffff'/%3E%3C/svg%3E");
background-repeat: no-repeat; background-position: center; background-size: contain; opacity: 0.8;
}
.player-wrapper .easyplayer-stretch:hover::after { opacity: 1; }
.player-wrapper.hide-btn-zoom .easyplayer-zoom, .player-wrapper.hide-btn-zoom .easyplayer-zoom-stop { display: none !important; }
.player-wrapper .easyplayer-zoom-controls {
position: absolute !important; top: 50px !important; left: 50% !important;
transform: translateX(-50%); z-index: 199 !important;
background: rgba(0,0,0,0.6); border-radius: 20px; padding: 0 10px;
}
.player-wrapper.force-hide-controls .easyplayer-controls { opacity: 0 !important; visibility: hidden !important; transition: opacity 0.3s ease; }
</style>