-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathVideoPlayer.vue
More file actions
369 lines (321 loc) · 9.57 KB
/
VideoPlayer.vue
File metadata and controls
369 lines (321 loc) · 9.57 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
<template>
<a-card v-if="visible && videoUrl" class="video-player-section">
<div class="player-header">
<h3>正在播放: {{ episodeName }}</h3>
<div class="player-controls">
<a-button @click="closePlayer" size="small" type="outline">
<template #icon>
<icon-close />
</template>
关闭播放器
</a-button>
</div>
</div>
<div class="video-player-container">
<video
ref="videoPlayer"
class="video-player"
controls
autoplay
preload="auto"
:poster="poster"
>
您的浏览器不支持视频播放
</video>
</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: ''
}
})
// Emits
const emit = defineEmits(['close', 'error'])
// 响应式数据
const videoPlayer = ref(null)
const hlsInstance = ref(null)
// 链接类型判断函数
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 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播放')
}
} 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()
}
}
// 关闭播放器
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')
}
// 监听视频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 (hlsInstance.value) {
hlsInstance.value.destroy()
hlsInstance.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;
}
.player-header h3 {
font-size: 18px;
font-weight: 600;
color: var(--color-text-1);
margin: 0;
}
.player-controls {
display: flex;
gap: 8px;
}
.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;
}
/* 响应式设计 */
@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>