EasyPlayer.vue
11 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
<template>
<div class="player-wrapper" @click="onPlayerClick">
<!-- 播放器容器 -->
<div :id="uniqueId" ref="container" class="player-box"></div>
<!-- 待机/无信号遮罩 -->
<!-- 【修改】增加 showCustomMask 判断,为 false 时不显示 -->
<div v-if="showCustomMask && !hasUrl && !isLoading && !isError" class="idle-mask"></div>
<!-- 状态蒙层 (Loading / Error) -->
<!-- 【修改】增加 showCustomMask 判断,为 false 时不显示 -->
<div v-if="showCustomMask && (isLoading || isError)" class="status-mask">
<div v-if="isLoading" class="loading-content">
<div class="loading-spinner"></div>
<div class="status-text">视频连接中...</div>
</div>
<div v-else-if="isError" class="error-content">
<div class="status-text error-text">{{ errorMessage }}</div>
<el-button type="primary" size="mini" icon="el-icon-refresh-right" @click.stop="handleRetry">重试</el-button>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'VideoPlayer',
props: {
initialPlayUrl: { type: [String, Object], default: '' },
initialBufferTime: { type: Number, default: 0.1 },
isResize: { type: Boolean, default: true },
loadTimeout: { type: Number, default: 20000 },
hasAudio: { type: Boolean, default: true },
// 【新增】是否显示自定义遮罩层
// true: 显示加载动画、错误提示、待机黑幕 (默认,适用于单路播放)
// false: 隐藏所有遮罩,直接显示播放器底层 (适用于轮播,减少视觉干扰)
showCustomMask: { type: Boolean, default: true }
},
data() {
return {
uniqueId: `player-box-${Date.now()}-${Math.floor(Math.random() * 1000)}`,
playerInstance: null,
isPlaying: false,
retryCount: 0,
isLoading: false,
isError: false,
errorMessage: '',
timeoutTimer: null,
checkVideoTimer: null,
statusPoller: null
};
},
computed: {
hasUrl() {
const url = this.initialPlayUrl;
if (!url) return false;
if (typeof url === 'string') return url.length > 0;
return !!url.videoUrl;
}
},
watch: {
initialPlayUrl(newUrl) {
const url = typeof newUrl === 'string' ? newUrl : (newUrl && newUrl.videoUrl) || '';
if (url) {
if (this.playerInstance) {
// 切换时清屏 (如果 showCustomMask=false,虽然不显示遮罩,但画面依然会变黑,体验更好)
this.clearScreen(true);
setTimeout(() => this.play(url), 10); // 从30ms减少到10ms,加快切换速度
} else {
this.create();
this.$nextTick(() => this.play(url));
}
} else {
this.pause();
this.clearScreen(false);
}
},
hasAudio() {
const url = typeof this.initialPlayUrl === 'string' ? this.initialPlayUrl : (this.initialPlayUrl && this.initialPlayUrl.videoUrl) || '';
if (url) {
this.destroyAndReplay(url);
}
}
},
mounted() {
this.$nextTick(() => {
setTimeout(() => {
this.create();
const url = typeof this.initialPlayUrl === 'string' ? this.initialPlayUrl : (this.initialPlayUrl && this.initialPlayUrl.videoUrl) || '';
if (url) {
this.play(url);
}
}, 50); // 从100ms减少到50ms,加快初始化
});
},
beforeDestroy() {
this.destroy();
},
methods: {
create() {
if (this.playerInstance) return;
const container = this.$refs.container;
if (!container) return;
if ((container.clientWidth === 0 || container.clientHeight === 0) && this.retryCount < 10) {
this.retryCount++;
return;
}
if (!window.EasyPlayerPro) {
this.triggerError('核心组件未加载');
return;
}
try {
container.innerHTML = '';
this.playerInstance = new window.EasyPlayerPro(container, {
bufferTime: 0.1, // 减小缓冲时间,加快启动
stretch: !this.isResize,
hasAudio: this.hasAudio,
videoBuffer: 0.1, // 减小视频缓冲,加快显示
isLive: true,
});
this.retryCount = 0;
// 库自带事件
this.playerInstance.on('playStart', () => {
this.isPlaying = true;
clearTimeout(this.timeoutTimer);
});
this.playerInstance.on('playError', (err) => {
console.error('播放错误:', err);
this.triggerError('播放发生错误');
});
this.playerInstance.on('timeupdate', () => this.onVideoContentReady());
this.bindNativeEvents();
} catch (e) {
console.warn("播放器实例创建未成功:", e.message);
this.playerInstance = null;
}
},
bindNativeEvents() {
const container = this.$refs.container;
if (!container) return;
if (this.checkVideoTimer) clearInterval(this.checkVideoTimer);
this.checkVideoTimer = setInterval(() => {
const videoEl = container.querySelector('video');
if (videoEl) {
clearInterval(this.checkVideoTimer);
this.checkVideoTimer = null;
videoEl.addEventListener('playing', () => this.onVideoContentReady());
videoEl.addEventListener('loadeddata', () => this.onVideoContentReady());
videoEl.addEventListener('timeupdate', () => {
if (videoEl.currentTime > 0) this.onVideoContentReady();
});
}
}, 100);
},
startVideoStatusPoller() {
if (this.statusPoller) clearInterval(this.statusPoller);
this.statusPoller = setInterval(() => {
if (!this.isLoading) {
clearInterval(this.statusPoller);
this.statusPoller = null;
return;
}
const container = this.$refs.container;
if (container) {
const videoEl = container.querySelector('video');
if (videoEl && (videoEl.currentTime > 0.1 || videoEl.readyState > 2)) {
this.onVideoContentReady();
}
}
}, 200);
},
clearScreen(showLoading = true) {
this.isLoading = showLoading;
this.isError = false;
this.errorMessage = '';
if (this.statusPoller) {
clearInterval(this.statusPoller);
this.statusPoller = null;
}
const container = this.$refs.container;
if (container) {
const videoEl = container.querySelector('video');
if (videoEl) {
videoEl.pause();
videoEl.src = "";
videoEl.removeAttribute('src');
videoEl.removeAttribute('poster');
videoEl.load();
}
}
},
onVideoContentReady() {
if (this.isLoading || this.isError) {
this.isLoading = false;
this.isError = false;
this.isPlaying = true;
this.errorMessage = '';
if (this.timeoutTimer) clearTimeout(this.timeoutTimer);
if (this.statusPoller) clearInterval(this.statusPoller);
}
},
play(url) {
const playUrl = url || this.initialPlayUrl;
if (!playUrl) return;
if (!this.playerInstance) {
this.create();
setTimeout(() => this.play(playUrl), 200);
return;
}
this.resetStatus();
this.isLoading = true;
this.timeoutTimer = setTimeout(() => {
if (!this.isPlaying) {
this.triggerError('视频加载超时');
}
}, this.loadTimeout);
this.playerInstance.play(playUrl)
.then(() => {
this.isPlaying = true;
clearTimeout(this.timeoutTimer);
})
.catch(e => {
console.error(`播放调用失败:`, e);
this.triggerError('无法连接流媒体');
});
this.bindNativeEvents();
this.startVideoStatusPoller();
},
triggerError(msg) {
this.isLoading = false;
this.isPlaying = false;
this.isError = true;
this.errorMessage = msg;
if (this.timeoutTimer) clearTimeout(this.timeoutTimer);
if (this.statusPoller) clearInterval(this.statusPoller);
},
resetStatus() {
this.isLoading = false;
this.isError = false;
this.errorMessage = '';
if (this.timeoutTimer) clearTimeout(this.timeoutTimer);
if (this.statusPoller) clearInterval(this.statusPoller);
},
handleRetry() {
this.destroyAndReplay(this.initialPlayUrl);
},
pause() {
if (this.playerInstance && this.isPlaying) {
this.playerInstance.pause();
this.isPlaying = false;
}
},
destroy() {
this.resetStatus();
if (this.checkVideoTimer) clearInterval(this.checkVideoTimer);
if (this.playerInstance) {
try {
this.playerInstance.destroy();
} catch (e) {
console.warn('播放器销毁失败:', e);
}
this.playerInstance = null;
this.isPlaying = false;
}
// 清除video元素,但保留容器以便下次播放
const container = this.$refs.container;
if (container) {
const videoEl = container.querySelector('video');
if (videoEl) {
videoEl.pause();
videoEl.src = '';
videoEl.load();
videoEl.remove();
}
}
},
destroyAndReplay(url) {
this.destroy();
this.clearScreen(true);
setTimeout(() => {
this.create();
let playUrlString = url;
if (typeof url === 'object' && url !== null) {
playUrlString = url.videoUrl;
}
this.play(playUrlString);
}, 200);
},
onPlayerClick() {
this.$emit('click');
},
},
};
</script>
<style scoped>
/* 确保 video 标签本身也是黑色背景 */
::v-deep video {
background: #000 !important;
object-fit: contain;
}
.player-wrapper {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
position: relative;
}
.player-box {
flex: 1;
width: 100%;
height: 100%;
background: #000 !important;
overflow: hidden;
position: relative;
transform: translate3d(0, 0, 0);
contain: strict;
}
.status-mask {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
z-index: 20;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
overflow: hidden;
}
.idle-mask {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: #000;
z-index: 15;
pointer-events: auto;
}
/* ... 保持原有样式 ... */
.loading-content, .error-content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 100%;
padding: 0 5px;
}
.loading-content {
gap: 10px;
}
.error-content {
gap: 5px;
}
.status-text {
font-size: 12px;
color: #fff;
line-height: 1.5;
word-break: break-all;
}
.error-text {
color: #ff6d6d;
margin-bottom: 2px;
}
.loading-spinner {
width: 24px;
height: 24px;
border: 2px solid rgba(255, 255, 255, 0.3);
border-radius: 50%;
border-top-color: #fff;
animation: spin 1s ease-in-out infinite;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
</style>