VideoPlayer.vue
10 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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
<template>
<div class="video-player-container">
<!-- 原生播放器 (iOS 微信等环境) -->
<video
v-if="useNativePlayer"
ref="nativeVideoRef"
class="video-player"
:src="videoUrlValue"
:autoplay="props.autoplay"
:muted="props.autoplay"
controls
playsinline
webkit-playsinline="true"
x5-playsinline="true"
x5-video-player-type="h5"
x5-video-player-fullscreen="true"
preload="metadata"
@play="handleNativePlay"
@pause="handleNativePause"
/>
<!-- Video.js 播放器 (PC/Android/其他环境) -->
<VideoPlayer
v-else
ref="videoRef"
:options="videoOptions"
playsinline
:class="['video-player', 'vjs-big-play-centered', { loading: !state }]"
@mounted="handleVideoJsMounted"
@play="handlePlay"
@pause="handlePause"
/>
<!-- <div v-if="showNetworkSpeedOverlay && !showErrorOverlay" class="speed-overlay">
<div class="speed-content">
<div
class="speed-title"
:class="{ 'speed-title-no-detail': !(networkSpeedText && networkSpeedText !== '未知') }"
>
网络较慢
</div>
<div v-if="networkSpeedText && networkSpeedText !== '未知'" class="speed-value">
{{ networkSpeedText.includes("Mbps") ? `当前网速:${networkSpeedText}` : `当前网络:${networkSpeedText}` }}
</div>
</div>
</div> -->
<div v-if="hlsDownloadSpeedText" class="hls-speed-badge">{{ hlsDownloadSpeedText }}</div>
<div v-if="props.debug" class="hls-debug-badge">
{{ hlsSpeedDebugText || "debug:empty" }}
</div>
<!-- 错误提示覆盖层 -->
<div v-if="showErrorOverlay" class="error-overlay">
<div class="error-content">
<div class="error-icon">⚠️</div>
<div class="error-message">{{ errorMessage }}</div>
<button v-if="canRetry" @click="retryLoad" class="retry-button">重试</button>
</div>
</div>
</div>
</template>
<script setup>
import { ref } from "vue";
import { VideoPlayer } from "@videojs-player/vue";
import "video.js/dist/video-js.css";
import { useVideoPlayer } from "@/composables/useVideoPlayer";
const props = defineProps({
options: {
type: Object,
required: false,
default: () => ({}),
},
videoUrl: {
type: String,
required: true,
},
videoId: {
type: String,
required: true,
},
autoplay: {
type: Boolean,
required: false,
default: true,
},
debug: {
type: Boolean,
default: false
},
/**
* iOS 环境下是否强制使用原生播放器
* 默认为 true (使用原生),设为 false 则尝试使用 Video.js
*/
useNativeOnIos: {
type: Boolean,
default: true
}
});
const emit = defineEmits(["onPlay", "onPause"]);
const videoRef = ref(null);
const nativeVideoRef = ref(null);
const {
player,
state,
useNativePlayer,
videoUrlValue,
videoOptions,
showErrorOverlay,
errorMessage,
canRetry,
showNetworkSpeedOverlay,
networkSpeedText,
hlsDownloadSpeedText,
hlsSpeedDebugText,
retryLoad,
handleVideoJsMounted,
tryNativePlay
} = useVideoPlayer(props, emit, videoRef, nativeVideoRef);
// 事件处理
/**
* @description 处理播放事件
* @param {Event|Object} payload - 事件对象或数据
*/
const handlePlay = (payload) => emit("onPlay", payload);
/**
* @description 处理暂停事件
* @param {Event|Object} payload - 事件对象或数据
*/
const handlePause = (payload) => emit("onPause", payload);
/**
* @description 处理原生播放事件
* @param {Event} event - 事件对象
*/
const handleNativePlay = (event) => emit("onPlay", event);
/**
* @description 处理原生暂停事件
* @param {Event} event - 事件对象
*/
const handleNativePause = (event) => emit("onPause", event);
// 暴露方法给父组件
defineExpose({
pause() {
if (useNativePlayer.value) {
try {
nativeVideoRef.value?.pause?.();
emit('onPause', nativeVideoRef.value);
} catch (e) {
}
return;
}
if (player.value && !player.value.isDisposed()) {
try {
player.value.pause();
emit('onPause', player.value);
} catch (e) {
}
}
},
play() {
if (useNativePlayer.value) {
tryNativePlay();
return;
}
player.value?.play()?.catch(() => {});
},
getPlayer() {
return useNativePlayer.value ? nativeVideoRef.value : player.value;
},
getId() {
return props.videoId || "meta_id";
},
});
</script>
<style scoped>
.video-player-container {
width: 100%;
height: 100%;
position: relative;
}
.video-player {
width: 100%;
height: 100%;
display: block;
aspect-ratio: 16/9;
background-color: #000;
}
.video-player.loading {
opacity: 0.6;
}
/* 错误覆盖层样式 */
.error-overlay {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.8);
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
}
.error-content {
text-align: center;
color: white;
padding: 14px;
max-width: 84%;
}
.error-icon {
font-size: 32px;
margin-bottom: 8px;
}
.error-message {
font-size: 14px;
margin-bottom: 12px;
line-height: 1.35;
}
.speed-overlay {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex;
align-items: center;
justify-content: center;
z-index: 900;
pointer-events: none;
}
.speed-content {
padding: 14px 18px;
border-radius: 12px;
background: rgba(0, 0, 0, 0.65);
backdrop-filter: blur(10px);
color: #fff;
text-align: center;
max-width: 80%;
}
.speed-title {
font-size: 16px;
font-weight: 600;
line-height: 1.2;
margin-bottom: 8px;
}
.speed-title-no-detail {
margin-bottom: 0;
}
.speed-value {
font-size: 14px;
line-height: 1.4;
opacity: 0.95;
}
.hls-speed-badge {
position: absolute;
right: 10px;
bottom: 10px;
z-index: 850;
pointer-events: none;
padding: 4px 8px;
border-radius: 8px;
background: rgba(0, 0, 0, 0.5);
color: #fff;
font-size: 12px;
line-height: 1.2;
}
.hls-debug-badge {
position: absolute;
left: 10px;
top: 10px;
z-index: 1200;
pointer-events: none;
padding: 4px 8px;
border-radius: 8px;
background: rgba(0, 0, 0, 0.45);
color: #fff;
font-size: 11px;
line-height: 1.2;
max-width: 90%;
white-space: pre-wrap;
word-break: break-all;
}
.retry-button {
background: #007bff;
color: white;
border: none;
padding: 8px 14px;
border-radius: 8px;
cursor: pointer;
font-size: 13px;
transition: background-color 0.3s;
}
.retry-button:hover {
background: #0056b3;
}
:deep(.vjs-big-play-button) {
display: none !important;
}
:deep(.vjs-error-display) {
display: none !important;
}
:deep(.vjs-modal-dialog) {
display: none !important;
}
/* 修复 Video.js 在移动端拖动进度条时的报错 */
:deep(.vjs-progress-control) {
touch-action: none;
}
:deep(.vjs-progress-holder) {
touch-action: none;
}
:deep(.video-js) {
/* 优化移动端交互 */
touch-action: manipulation;
}
/* 倍速播放控件样式优化 */
:deep(.vjs-playback-rate) {
order: 7;
position: relative;
display: flex !important;
align-items: center;
z-index: 10;
margin-right: 12px;
}
/* 隐藏可能存在的图标 */
:deep(.vjs-playback-rate .vjs-icon-chapters) {
display: none !important;
}
:deep(.vjs-playback-rate .vjs-playback-rate-value) {
font-size: 1.5em;
line-height: 2;
color: #fff;
background: transparent;
border-radius: 4px;
padding: 0 8px;
margin: 0;
transition: all 0.3s ease;
cursor: pointer;
min-width: auto;
text-align: center;
width: auto;
display: inline-block;
}
:deep(.vjs-playback-rate:hover .vjs-playback-rate-value) {
background: transparent;
transform: scale(1.05);
}
/* 菜单容器优化 - 解决遮挡问题 */
:deep(.vjs-playback-rate .vjs-menu) {
background: rgba(0, 0, 0, 0.95);
border-radius: 8px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
backdrop-filter: blur(15px);
border: 1px solid rgba(255, 255, 255, 0.1);
position: absolute;
bottom: 100%;
right: 0;
margin-bottom: 8px;
min-width: 80px;
max-height: 200px;
overflow-y: auto;
z-index: 1000;
}
/* 确保菜单在视口内显示 */
:deep(.vjs-playback-rate .vjs-menu.vjs-lock-showing) {
display: block !important;
opacity: 1 !important;
visibility: visible !important;
}
:deep(.vjs-playback-rate .vjs-menu-content) {
padding: 4px 0;
}
:deep(.vjs-playback-rate .vjs-menu-item) {
color: #fff;
padding: 10px 16px;
font-size: 14px;
transition: all 0.2s ease;
border-radius: 4px;
margin: 2px 4px;
cursor: pointer;
white-space: nowrap;
text-align: center;
}
:deep(.vjs-playback-rate .vjs-menu-item:hover) {
background: rgba(255, 255, 255, 0.15);
transform: translateX(2px);
}
:deep(.vjs-playback-rate .vjs-menu-item.vjs-selected) {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: #fff;
font-weight: 600;
}
/* 移动端优化 */
@media (max-width: 768px) {
:deep(.vjs-playback-rate) {
display: flex !important;
visibility: visible !important;
margin-right: 8px;
}
:deep(.vjs-playback-rate .vjs-playback-rate-value) {
font-size: 1.3em;
padding: 0 6px;
min-width: auto;
height: 36px;
line-height: 36px;
width: auto;
margin: 0;
}
:deep(.vjs-playback-rate .vjs-menu) {
min-width: 100px;
max-height: 180px;
bottom: 120%;
right: -10px;
}
:deep(.vjs-playback-rate .vjs-menu-item) {
padding: 12px 16px;
font-size: 16px;
min-height: 44px;
display: flex;
align-items: center;
justify-content: center;
}
}
/* 小屏幕设备进一步优化 */
@media (max-width: 480px) {
:deep(.vjs-playback-rate) {
margin-right: 6px;
}
:deep(.vjs-playback-rate .vjs-playback-rate-value) {
font-size: 1.2em;
padding: 0 4px;
min-width: auto;
height: 32px;
line-height: 32px;
width: auto;
margin: 0;
}
:deep(.vjs-playback-rate .vjs-menu) {
min-width: 90px;
right: -5px;
}
:deep(.vjs-playback-rate .vjs-menu-item) {
padding: 10px 12px;
font-size: 15px;
min-height: 40px;
}
}
</style>