-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathVideoPlayer.vue
More file actions
714 lines (621 loc) · 18.1 KB
/
VideoPlayer.vue
File metadata and controls
714 lines (621 loc) · 18.1 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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
<template>
<a-card v-if="visible && videoUrl" class="video-player-section">
<div class="player-header">
<h3>正在播放: {{ episodeName }}</h3>
<div class="player-controls">
<div class="compact-button-group">
<div class="compact-btn" :class="{ active: autoNext }" @click="toggleAutoNext">
<svg class="btn-icon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M5 3l14 9-14 9V3z" fill="currentColor"/>
<path d="M19 3v18" stroke="currentColor" stroke-width="2"/>
</svg>
<span class="btn-text">自动连播</span>
</div>
<div class="compact-btn" :class="{ active: showCountdown }" @click="toggleCountdown">
<svg class="btn-icon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="2"/>
<polyline points="12,6 12,12 16,14" stroke="currentColor" stroke-width="2"/>
</svg>
<span class="btn-text">倒计时</span>
</div>
<div class="compact-btn selector-btn">
<svg class="btn-icon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect x="3" y="3" width="18" height="18" rx="2" ry="2" stroke="currentColor" stroke-width="2"/>
<circle cx="8.5" cy="8.5" r="1.5" fill="currentColor"/>
<path d="m21 15-3.086-3.086a2 2 0 0 0-2.828 0L6 21" stroke="currentColor" stroke-width="2"/>
</svg>
<a-select
:model-value="playerType"
@change="handlePlayerTypeChange"
class="compact-select"
size="small"
>
<a-option value="default">默认播放器</a-option>
<a-option value="artplayer">ArtPlayer</a-option>
</a-select>
</div>
<div class="compact-btn close-btn" @click="closePlayer">
<svg class="btn-icon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<line x1="18" y1="6" x2="6" y2="18" stroke="currentColor" stroke-width="2"/>
<line x1="6" y1="6" x2="18" y2="18" stroke="currentColor" stroke-width="2"/>
</svg>
<span class="btn-text">关闭</span>
</div>
</div>
</div>
</div>
<div class="video-player-container">
<video
ref="videoPlayer"
class="video-player"
controls
autoplay
preload="auto"
:poster="poster"
>
您的浏览器不支持视频播放
</video>
<!-- 自动下一集倒计时弹窗 -->
<div v-if="showAutoNextDialog" class="auto-next-dialog">
<div class="auto-next-content">
<div class="auto-next-title">
<span>即将播放下一集</span>
</div>
<div class="auto-next-episode" v-if="getNextEpisode()">
{{ getNextEpisode().name }}
</div>
<div class="auto-next-countdown">
{{ autoNextCountdown }} 秒后自动播放
</div>
<div class="auto-next-buttons">
<button @click="playNextEpisode" class="btn-play-now">立即播放</button>
<button @click="cancelAutoNext" class="btn-cancel">取消</button>
</div>
</div>
</div>
</div>
</a-card>
</template>
<script setup>
import { ref, watch, onMounted, onUnmounted, nextTick } from 'vue'
import { Message } from '@arco-design/web-vue'
import { IconClose } from '@arco-design/web-vue/es/icon'
import Hls from 'hls.js'
// Props
const props = defineProps({
visible: {
type: Boolean,
default: false
},
videoUrl: {
type: String,
default: ''
},
episodeName: {
type: String,
default: '未知选集'
},
poster: {
type: String,
default: ''
},
playerType: {
type: String,
default: 'default'
},
// 自动下一集功能相关 props
episodes: {
type: Array,
default: () => []
},
currentEpisodeIndex: {
type: Number,
default: 0
}
})
// Emits
const emit = defineEmits(['close', 'error', 'player-change', 'next-episode'])
// 响应式数据
const videoPlayer = ref(null)
const hlsInstance = ref(null)
const autoNext = ref(true) // 默认开启自动连播
const showCountdown = ref(false)
const showAutoNextDialog = ref(false)
const autoNextCountdown = ref(10)
const countdownTimer = ref(null)
const isProcessingAutoNext = ref(false) // 防止重复触发自动连播
// 切换自动连播
const toggleAutoNext = () => {
autoNext.value = !autoNext.value
}
// 切换倒计时显示
const toggleCountdown = () => {
showCountdown.value = !showCountdown.value
}
// 检查是否有下一集
const hasNextEpisode = () => {
return props.episodes && props.episodes.length > 0 &&
props.currentEpisodeIndex < props.episodes.length - 1
}
// 获取下一集信息
const getNextEpisode = () => {
if (hasNextEpisode()) {
return props.episodes[props.currentEpisodeIndex + 1]
}
return null
}
// 播放下一集
const playNextEpisode = () => {
if (hasNextEpisode()) {
const nextIndex = props.currentEpisodeIndex + 1
emit('next-episode', nextIndex)
hideAutoNextDialog()
// 重置防抖标志
setTimeout(() => {
isProcessingAutoNext.value = false
}, 2000) // 2秒后重置,给视频切换足够的时间
}
}
// 显示自动下一集对话框
const showAutoNextDialogFunc = () => {
if (!autoNext.value || !hasNextEpisode()) return
showAutoNextDialog.value = true
autoNextCountdown.value = 10
countdownTimer.value = setInterval(() => {
autoNextCountdown.value--
if (autoNextCountdown.value <= 0) {
playNextEpisode()
}
}, 1000)
}
// 隐藏自动下一集对话框
const hideAutoNextDialog = () => {
showAutoNextDialog.value = false
if (countdownTimer.value) {
clearInterval(countdownTimer.value)
countdownTimer.value = null
}
}
// 取消自动下一集
const cancelAutoNext = () => {
hideAutoNextDialog()
// 重置防抖标志
isProcessingAutoNext.value = false
}
// 链接类型判断函数
const isDirectVideoLink = (url) => {
if (!url) return false
// 视频文件扩展名
const videoExtensions = [
'.mp4', '.webm', '.ogg', '.avi', '.mov', '.wmv', '.flv', '.mkv',
'.m4v', '.3gp', '.ts', '.m3u8', '.mpd'
]
// 检查URL是否包含视频扩展名
const hasVideoExtension = videoExtensions.some(ext =>
url.toLowerCase().includes(ext)
)
// 检查是否是流媒体格式
const isStreamingFormat = url.toLowerCase().includes('m3u8') ||
url.toLowerCase().includes('mpd') ||
url.toLowerCase().includes('rtmp') ||
url.toLowerCase().includes('rtsp')
// 检查是否看起来像网页链接
const looksLikeWebpage = url.includes('://') &&
(url.includes('.html') ||
url.includes('.php') ||
url.includes('.asp') ||
url.includes('.jsp') ||
url.match(/\/[^.]*$/) || // 没有扩展名的路径
url.includes('?') || // 包含查询参数
url.includes('#')) // 包含锚点
// 如果有视频扩展名或是流媒体格式,认为是直链
if (hasVideoExtension || isStreamingFormat) {
return true
}
// 如果看起来像网页,认为不是直链
if (looksLikeWebpage && !hasVideoExtension && !isStreamingFormat) {
return false
}
// 默认尝试作为直链处理
return true
}
// 初始化视频播放器
const initVideoPlayer = (url) => {
if (!videoPlayer.value || !url) return
console.log('初始化视频播放器:', url)
// 首先判断链接类型
if (!isDirectVideoLink(url)) {
console.log('检测到网页链接,在新窗口打开:', url)
Message.info('检测到网页链接,正在新窗口打开...')
window.open(url, '_blank')
emit('close') // 关闭播放器
return
}
// 清理之前的HLS实例
if (hlsInstance.value) {
hlsInstance.value.destroy()
hlsInstance.value = null
}
const video = videoPlayer.value
// 视频结束事件处理函数
const handleVideoEnded = () => {
console.log('视频播放结束')
// 防抖:如果正在处理自动连播,则忽略
if (isProcessingAutoNext.value) {
console.log('正在处理自动连播,忽略重复的ended事件')
return
}
if (autoNext.value && hasNextEpisode()) {
isProcessingAutoNext.value = true
if (showCountdown.value) {
showAutoNextDialogFunc()
} else {
// 如果不显示倒计时,直接播放下一集
setTimeout(() => {
playNextEpisode()
}, 1000)
}
}
}
// 检测视频格式
const isM3u8 = url.toLowerCase().includes('.m3u8') || url.toLowerCase().includes('m3u8')
if (isM3u8) {
// 处理m3u8格式
if (Hls.isSupported()) {
// 使用HLS.js播放m3u8
const hls = new Hls({
enableWorker: true,
lowLatencyMode: true,
backBufferLength: 90
})
hls.loadSource(url)
hls.attachMedia(video)
hls.on(Hls.Events.MANIFEST_PARSED, () => {
console.log('HLS manifest 解析完成,开始播放')
video.play().catch(err => {
console.warn('自动播放失败:', err)
})
})
hls.on(Hls.Events.ERROR, (event, data) => {
console.error('HLS播放错误:', data)
if (data.fatal) {
switch (data.type) {
case Hls.ErrorTypes.NETWORK_ERROR:
Message.error('网络错误,请检查网络连接')
hls.startLoad()
break
case Hls.ErrorTypes.MEDIA_ERROR:
Message.error('媒体错误,尝试恢复播放')
hls.recoverMediaError()
break
default:
// 播放器错误时,检查是否为网页链接
if (!isDirectVideoLink(url)) {
console.log('HLS播放失败,检测到可能是网页链接,在新窗口打开:', url)
Message.info('视频播放失败,检测到网页链接,正在新窗口打开...')
window.open(url, '_blank')
emit('close') // 关闭播放器
} else {
Message.error('播放器错误,请重试')
emit('error', '播放器错误')
}
hls.destroy()
break
}
}
})
hlsInstance.value = hls
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
// Safari原生支持HLS
video.src = url
video.addEventListener('loadedmetadata', () => {
video.play().catch(err => {
console.warn('自动播放失败:', err)
})
})
} else {
Message.error('您的浏览器不支持HLS播放')
emit('error', '浏览器不支持HLS播放')
return // 如果不支持HLS,直接返回,不添加事件监听器
}
} else {
// 处理其他格式的视频(mp4, webm, avi等)
console.log('播放普通视频格式:', url)
video.src = url
// 添加事件监听器
const handleLoadedMetadata = () => {
console.log('视频元数据加载完成,开始播放')
video.play().catch(err => {
console.warn('自动播放失败:', err)
Message.warning('自动播放失败,请手动点击播放')
})
}
const handleError = (e) => {
console.error('视频播放错误:', e)
// 如果播放失败,再次检查是否为网页链接
if (!isDirectVideoLink(url)) {
console.log('播放失败,检测到可能是网页链接,在新窗口打开:', url)
Message.info('视频播放失败,检测到网页链接,正在新窗口打开...')
window.open(url, '_blank')
emit('close') // 关闭播放器
} else {
Message.error('视频播放失败,请检查视频链接或格式')
emit('error', '视频播放失败')
}
}
const handleLoadStart = () => {
console.log('开始加载视频')
}
// 移除之前的事件监听器(如果有)
video.removeEventListener('loadedmetadata', handleLoadedMetadata)
video.removeEventListener('error', handleError)
video.removeEventListener('loadstart', handleLoadStart)
// 添加新的事件监听器
video.addEventListener('loadedmetadata', handleLoadedMetadata)
video.addEventListener('error', handleError)
video.addEventListener('loadstart', handleLoadStart)
// 开始加载视频
video.load()
}
// 统一添加视频结束事件监听器(避免重复添加)
video.removeEventListener('ended', handleVideoEnded)
video.addEventListener('ended', handleVideoEnded)
}
// 关闭播放器
const closePlayer = () => {
console.log('关闭视频播放器')
// 停止播放
if (videoPlayer.value) {
videoPlayer.value.pause()
videoPlayer.value.currentTime = 0
}
// 清理HLS实例
if (hlsInstance.value) {
hlsInstance.value.destroy()
hlsInstance.value = null
}
emit('close')
}
// 处理播放器类型变更
const handlePlayerTypeChange = (newType) => {
emit('player-change', newType)
}
// 监听视频URL变化
watch(() => props.videoUrl, (newUrl) => {
if (newUrl && props.visible) {
nextTick(() => {
initVideoPlayer(newUrl)
})
}
}, { immediate: true })
// 监听显示状态变化
watch(() => props.visible, (newVisible) => {
if (newVisible && props.videoUrl) {
nextTick(() => {
initVideoPlayer(props.videoUrl)
})
} else if (!newVisible) {
// 隐藏时清理资源
if (hlsInstance.value) {
hlsInstance.value.destroy()
hlsInstance.value = null
}
}
})
// 组件卸载时清理资源
onUnmounted(() => {
console.log('VideoPlayer组件卸载,清理播放器资源')
// 清理视频播放器
if (videoPlayer.value) {
videoPlayer.value.pause()
videoPlayer.value.src = ''
videoPlayer.value.load() // 这会清理所有事件监听器
}
if (hlsInstance.value) {
hlsInstance.value.destroy()
hlsInstance.value = null
}
// 清理倒计时定时器
if (countdownTimer.value) {
clearInterval(countdownTimer.value)
countdownTimer.value = null
}
})
</script>
<style scoped>
/* 视频播放器样式 */
.video-player-section {
margin-bottom: 20px;
border-radius: 12px;
overflow: hidden;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}
.player-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 16px;
padding: 12px 16px;
/* background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%); */
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
.player-header h3 {
margin: 0;
color: #2c3e50;
font-size: 16px;
font-weight: 600;
}
.player-controls {
display: flex;
align-items: center;
gap: 8px;
}
.compact-button-group {
display: flex;
align-items: center;
gap: 4px;
}
.compact-btn {
display: flex;
align-items: center;
gap: 6px;
padding: 4px 8px;
height: 28px;
background: white;
border: 1px solid #d9d9d9;
border-radius: 4px;
cursor: pointer;
transition: all 0.2s ease;
font-size: 12px;
color: #666;
}
.compact-btn:hover {
border-color: #1890ff;
color: #1890ff;
}
.compact-btn.active {
background: #1890ff;
border-color: #1890ff;
color: white;
}
.compact-btn.close-btn {
background: #ff4d4f;
border-color: #ff4d4f;
color: white;
}
.compact-btn.close-btn:hover {
background: #ff7875;
border-color: #ff7875;
}
.btn-icon {
width: 14px;
height: 14px;
flex-shrink: 0;
}
.btn-text {
font-size: 12px;
white-space: nowrap;
}
.compact-select {
border: none !important;
background: transparent !important;
box-shadow: none !important;
font-size: 12px;
min-width: 80px;
}
.compact-select .arco-select-view-single {
border: none !important;
background: transparent !important;
padding: 0 !important;
height: auto !important;
min-height: auto !important;
}
.selector-btn {
min-width: 120px;
}
.video-player-container {
position: relative;
width: 100%;
background: #000;
border-radius: 8px;
overflow: hidden;
}
.video-player {
width: 100%;
height: auto;
min-height: 400px;
max-height: 70vh;
background: #000;
outline: none;
}
.video-player::-webkit-media-controls-panel {
background-color: transparent;
}
.video-player::-webkit-media-controls-play-button,
.video-player::-webkit-media-controls-volume-slider,
.video-player::-webkit-media-controls-timeline,
.video-player::-webkit-media-controls-current-time-display,
.video-player::-webkit-media-controls-time-remaining-display {
color: #fff;
}
/* 自动下一集倒计时弹窗样式 */
.auto-next-dialog {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(0, 0, 0, 0.9);
border-radius: 12px;
padding: 24px;
z-index: 1000;
min-width: 300px;
text-align: center;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
backdrop-filter: blur(10px);
}
.auto-next-content {
display: flex;
flex-direction: column;
gap: 12px;
}
.auto-next-title {
font-size: 16px;
font-weight: 600;
color: #fff;
}
.auto-next-episode {
font-size: 14px;
color: #23ade5;
font-weight: 500;
}
.auto-next-countdown {
font-size: 18px;
font-weight: bold;
color: #ff6b6b;
}
.auto-next-buttons {
display: flex;
gap: 12px;
justify-content: center;
margin-top: 8px;
}
.btn-play-now,
.btn-cancel {
padding: 8px 16px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
font-weight: 500;
transition: all 0.2s ease;
}
.btn-play-now {
background: #23ade5;
color: white;
}
.btn-play-now:hover {
background: #1890d5;
}
.btn-cancel {
background: #666;
color: white;
}
.btn-cancel:hover {
background: #555;
}
/* 响应式设计 */
@media (max-width: 768px) {
.player-header {
flex-direction: column;
gap: 8px;
align-items: flex-start;
}
.player-header h3 {
font-size: 14px;
}
.video-player {
min-height: 200px;
}
}
</style>