VideoViewer.vue
18.2 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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
<template>
<div class="video-viewer">
<div class="video-toolbar">
<div class="toolbar-left">
<el-button-group>
<el-button
size="small"
:icon="isPlaying ? VideoPause : VideoPlay"
@click="togglePlay"
:title="isPlaying ? '暂停' : '播放'"
/>
<el-button
size="small"
:icon="RefreshLeft"
@click="skipBackward"
title="后退10秒"
/>
<el-button
size="small"
:icon="RefreshRight"
@click="skipForward"
title="前进10秒"
/>
</el-button-group>
<div class="time-display ml-10">
{{ formatTime(currentTime) }} / {{ formatTime(duration) }}
</div>
<div class="volume-control ml-10">
<el-button
size="small"
:icon="isMuted ? TurnOff : Open"
@click="toggleMute"
:title="isMuted ? '取消静音' : '静音'"
/>
<el-slider
v-model="volume"
:min="0"
:max="100"
@input="updateVolume"
class="volume-slider"
/>
</div>
<el-button
size="small"
:icon="FullScreen"
@click="toggleFullscreen"
title="全屏"
class="ml-10"
/>
</div>
<div class="toolbar-right">
<el-button size="small" :icon="Download" @click="downloadFile"
>下载</el-button
>
</div>
</div>
<div class="video-container" ref="videoContainerRef">
<div v-if="loading" class="loading-container">
<el-skeleton :rows="10" animated />
</div>
<div v-else-if="error" class="error-container">
<el-icon class="error-icon"><WarningFilled /></el-icon>
<p>{{ error }}</p>
<el-button size="small" type="primary" @click="loadVideo"
>重试</el-button
>
</div>
<div v-else class="video-player">
<video
ref="videoElementRef"
class="video-element"
:src="videoUrl"
@loadedmetadata="onLoadedMetadata"
@timeupdate="onTimeUpdate"
@ended="onEnded"
@error="onError"
@canplay="onCanPlay"
@play="onPlay"
@pause="onPause"
@click="togglePlay"
:poster="posterUrl"
/>
<div
class="video-controls"
v-show="showControls"
@mouseenter="resetControlsTimer"
@mouseleave="startHideControlsTimer"
>
<div class="progress-bar">
<el-slider
v-model="currentTime"
:min="0"
:max="duration || 100"
:format-tooltip="formatTime"
@input="seek"
class="progress-slider"
/>
</div>
<div class="control-buttons">
<div class="left-controls">
<el-button
size="small"
:icon="isPlaying ? VideoPause : VideoPlay"
@click.stop="togglePlay"
/>
<span class="time-display"
>{{ formatTime(currentTime) }} /
{{ formatTime(duration) }}</span
>
</div>
<div class="right-controls">
<div class="playback-speed">
<el-dropdown trigger="click" @command="setPlaybackRate">
<el-button size="small">
{{ playbackRate }}x
<el-icon class="el-icon--right"><ArrowDown /></el-icon>
</el-button>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item :command="0.5">0.5x</el-dropdown-item>
<el-dropdown-item :command="1.0">1.0x</el-dropdown-item>
<el-dropdown-item :command="1.5">1.5x</el-dropdown-item>
<el-dropdown-item :command="2.0">2.0x</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
</div>
<div class="volume-control-compact">
<el-button
size="small"
:icon="isMuted ? TurnOff : Open"
@click.stop="toggleMute"
/>
<el-slider
v-model="volume"
:min="0"
:max="100"
@input="updateVolume"
class="volume-slider-compact"
/>
</div>
<el-button
size="small"
:icon="FullScreen"
@click.stop="toggleFullscreen"
/>
</div>
</div>
</div>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from "vue";
import { ElMessage } from "element-plus";
import {
VideoPlay,
VideoPause,
RefreshLeft,
RefreshRight,
TurnOff,
Open,
Download,
FullScreen,
WarningFilled,
ArrowDown,
} from "@element-plus/icons-vue";
import { downloadFile as apiDownloadFile } from "@/api/files";
interface Props {
fileId: string | number;
fileName: string;
}
interface Emits {
(e: "mounted"): void;
}
const props = withDefaults(defineProps<Props>(), {
fileName: "未命名视频",
});
const emit = defineEmits<Emits>();
// 状态管理
const loading = ref(true);
const error = ref<string | null>(null);
const videoUrl = ref("");
const posterUrl = ref(""); // 视频封面URL
const isPlaying = ref(false);
const isMuted = ref(false);
const duration = ref(0);
const currentTime = ref(0);
const volume = ref(100);
const playbackRate = ref(1.0);
const isFullscreen = ref(false);
const showControls = ref(true);
const controlsTimer = ref<number | null>(null);
// 元素引用
const videoElementRef = ref<HTMLVideoElement | null>(null);
const videoContainerRef = ref<HTMLDivElement | null>(null);
// 加载视频
const loadVideo = async () => {
try {
loading.value = true;
error.value = null;
if (videoElementRef.value) {
videoElementRef.value.pause();
videoElementRef.value.src = "";
videoElementRef.value.load();
}
if (videoUrl.value) {
URL.revokeObjectURL(videoUrl.value);
}
// 下载文件
const response = await apiDownloadFile(props.fileId);
let blob = response;
if (response && (response as any).code === 200 && (response as any).data) {
blob = (response as any).data;
} else if (response && (response as any).data) {
blob = (response as any).data;
}
// 创建一个可播放的URL
videoUrl.value = URL.createObjectURL(blob as unknown as Blob);
} catch (err) {
console.error("加载视频文件失败:", err);
error.value = "加载视频文件失败,请重试";
} finally {
loading.value = false;
}
};
// 视频事件处理
const onLoadedMetadata = () => {
if (videoElementRef.value) {
duration.value = videoElementRef.value.duration;
// 尝试获取视频的第一帧作为封面
try {
const video = videoElementRef.value;
// 保存当前时间
const savedTime = video.currentTime;
// 设置为0来获取第一帧
video.currentTime = 0;
// 当视频可以播放时,创建封面
const createPoster = () => {
try {
// 创建canvas并绘制视频帧
const canvas = document.createElement("canvas");
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
const ctx = canvas.getContext("2d");
if (ctx) {
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
// 转换为图片URL
posterUrl.value = canvas.toDataURL("image/jpeg");
// 恢复时间
video.currentTime = savedTime;
}
} catch (e) {
console.error("创建视频封面失败:", e);
}
// 移除事件监听器
video.removeEventListener("canplay", createPoster);
};
video.addEventListener("canplay", createPoster);
} catch (e) {
console.error("无法创建视频封面:", e);
}
}
};
const onTimeUpdate = () => {
if (videoElementRef.value) {
currentTime.value = videoElementRef.value.currentTime;
}
};
const onEnded = () => {
isPlaying.value = false;
};
const onError = () => {
error.value = "视频播放失败,请重试";
isPlaying.value = false;
};
const onCanPlay = () => {
loading.value = false;
};
const onPlay = () => {
isPlaying.value = true;
};
const onPause = () => {
isPlaying.value = false;
};
// 播放控制
const togglePlay = () => {
const video = videoElementRef.value;
if (!video) return;
if (isPlaying.value) {
video.pause();
} else {
video.play().catch((err) => {
console.error("播放失败:", err);
error.value = "播放失败,请重试";
});
}
};
const toggleMute = () => {
const video = videoElementRef.value;
if (!video) return;
video.muted = !video.muted;
isMuted.value = video.muted;
};
const updateVolume = () => {
const video = videoElementRef.value;
if (!video) return;
video.volume = volume.value / 100;
if (volume.value === 0) {
video.muted = true;
isMuted.value = true;
} else if (isMuted.value) {
video.muted = false;
isMuted.value = false;
}
};
const seek = (time: number) => {
const video = videoElementRef.value;
if (!video) return;
video.currentTime = time;
};
const skipBackward = () => {
const video = videoElementRef.value;
if (!video) return;
video.currentTime = Math.max(0, video.currentTime - 10);
};
const skipForward = () => {
const video = videoElementRef.value;
if (!video) return;
video.currentTime = Math.min(duration.value, video.currentTime + 10);
};
const setPlaybackRate = (rate: number) => {
const video = videoElementRef.value;
if (!video) return;
video.playbackRate = rate;
playbackRate.value = rate;
};
// 时间格式化
const formatTime = (seconds: number): string => {
seconds = Math.floor(seconds);
const hours = Math.floor(seconds / 3600);
seconds = seconds % 3600;
const minutes = Math.floor(seconds / 60);
seconds = seconds % 60;
if (hours > 0) {
return `${hours}:${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
} else {
return `${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
}
};
// 全屏控制
const toggleFullscreen = () => {
if (!isFullscreen.value) {
enterFullscreen();
} else {
exitFullscreen();
}
};
const enterFullscreen = () => {
const elem = videoContainerRef.value;
if (!elem) return;
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if ((elem as any).mozRequestFullScreen) {
(elem as any).mozRequestFullScreen();
} else if ((elem as any).webkitRequestFullscreen) {
(elem as any).webkitRequestFullscreen();
} else if ((elem as any).msRequestFullscreen) {
(elem as any).msRequestFullscreen();
}
};
const exitFullscreen = () => {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if ((document as any).mozCancelFullScreen) {
(document as any).mozCancelFullScreen();
} else if ((document as any).webkitExitFullscreen) {
(document as any).webkitExitFullscreen();
} else if ((document as any).msExitFullscreen) {
(document as any).msExitFullscreen();
}
};
const fullscreenChangeHandler = () => {
isFullscreen.value = !!(
document.fullscreenElement ||
(document as any).webkitFullscreenElement ||
(document as any).mozFullScreenElement ||
(document as any).msFullscreenElement
);
};
// 控制栏显示/隐藏
const handleMouseMove = () => {
showControls.value = true;
resetControlsTimer();
};
const startHideControlsTimer = () => {
if (controlsTimer.value) {
clearTimeout(controlsTimer.value);
}
controlsTimer.value = window.setTimeout(() => {
if (isPlaying.value) {
showControls.value = false;
}
}, 3000);
};
const resetControlsTimer = () => {
if (controlsTimer.value) {
clearTimeout(controlsTimer.value);
}
startHideControlsTimer();
};
// 键盘快捷键
const handleKeyPress = (event: KeyboardEvent) => {
if (document.activeElement === document.body) {
switch (event.key) {
case " ": // 空格键
togglePlay();
event.preventDefault();
break;
case "ArrowLeft": // 左箭头
skipBackward();
event.preventDefault();
break;
case "ArrowRight": // 右箭头
skipForward();
event.preventDefault();
break;
case "ArrowUp": // 上箭头
volume.value = Math.min(100, volume.value + 5);
updateVolume();
event.preventDefault();
break;
case "ArrowDown": // 下箭头
volume.value = Math.max(0, volume.value - 5);
updateVolume();
event.preventDefault();
break;
case "m": // M键静音
case "M":
toggleMute();
event.preventDefault();
break;
case "f": // F键全屏
case "F":
toggleFullscreen();
event.preventDefault();
break;
}
}
};
// 下载文件
const downloadFile = async () => {
try {
const response = await apiDownloadFile(props.fileId);
let blob = response;
if (response && (response as any).code === 200 && (response as any).data) {
blob = (response as any).data;
} else if (response && (response as any).data) {
blob = (response as any).data;
}
const url = window.URL.createObjectURL(blob as unknown as Blob);
const a = document.createElement("a");
a.href = url;
a.download = props.fileName;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
ElMessage.success("文件下载成功");
} catch (err: any) {
ElMessage.error(`文件下载失败: ${err.message}`);
}
};
// 生命周期
onMounted(() => {
loadVideo();
// 监听鼠标移动事件,以显示/隐藏控件
document.addEventListener("mousemove", handleMouseMove);
// 监听全屏变化事件
document.addEventListener("fullscreenchange", fullscreenChangeHandler);
document.addEventListener("webkitfullscreenchange", fullscreenChangeHandler);
document.addEventListener("mozfullscreenchange", fullscreenChangeHandler);
document.addEventListener("MSFullscreenChange", fullscreenChangeHandler);
// 监听键盘事件
window.addEventListener("keydown", handleKeyPress);
emit("mounted");
});
onBeforeUnmount(() => {
// 清理资源
if (videoElementRef.value) {
videoElementRef.value.pause();
videoElementRef.value.src = "";
videoElementRef.value.load();
}
if (videoUrl.value) {
URL.revokeObjectURL(videoUrl.value);
}
// 移除事件监听器
document.removeEventListener("mousemove", handleMouseMove);
document.removeEventListener("fullscreenchange", fullscreenChangeHandler);
document.removeEventListener(
"webkitfullscreenchange",
fullscreenChangeHandler,
);
document.removeEventListener("mozfullscreenchange", fullscreenChangeHandler);
document.removeEventListener("MSFullscreenChange", fullscreenChangeHandler);
window.removeEventListener("keydown", handleKeyPress);
// 清除计时器
if (controlsTimer.value) {
clearTimeout(controlsTimer.value);
}
});
</script>
<style scoped lang="scss">
.video-viewer {
display: flex;
flex-direction: column;
height: 100%;
background: var(--color-bg, #141518);
color: var(--color-text, #eee);
overflow: hidden;
}
.video-toolbar {
padding: 8px 16px;
display: flex;
justify-content: space-between;
align-items: center;
background: var(--color-card, #232324);
border-bottom: 1px solid var(--color-border, #333);
flex-wrap: wrap;
gap: 8px;
z-index: 10;
}
.ml-10 {
margin-left: 10px;
}
.file-info {
font-size: 14px;
color: var(--color-text-secondary, #999);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
max-width: 300px;
text-align: center;
}
.toolbar-left,
.toolbar-right {
display: flex;
align-items: center;
gap: 8px;
}
.time-display {
font-size: 14px;
color: var(--color-text-secondary, #999);
min-width: 100px;
text-align: center;
}
.volume-control {
display: flex;
align-items: center;
gap: 8px;
width: 120px;
}
.volume-slider {
width: 80px;
}
.video-container {
flex: 1;
position: relative;
overflow: hidden;
background: #000;
}
.video-player {
width: 100%;
height: 100%;
position: relative;
}
.video-element {
width: 100%;
height: 100%;
object-fit: contain;
cursor: pointer;
background: var(--color-bg);
}
.video-controls {
position: absolute;
bottom: 0;
left: 0;
right: 0;
padding: 10px;
transition: opacity 0.3s ease;
}
.progress-bar {
margin-bottom: 10px;
}
.progress-slider {
width: 100%;
}
.control-buttons {
display: flex;
justify-content: space-between;
align-items: center;
}
.left-controls,
.right-controls {
display: flex;
align-items: center;
gap: 10px;
}
.playback-speed {
margin: 0 10px;
}
.volume-control-compact {
display: flex;
align-items: center;
gap: 5px;
}
.volume-slider-compact {
width: 60px;
}
.loading-container,
.error-container {
width: 100%;
height: 100%;
padding: 20px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
box-sizing: border-box;
background: var(--color-bg);
}
.error-container {
color: var(--color-danger, #ff4d4f);
text-align: center;
.error-icon {
font-size: 48px;
margin-bottom: 16px;
}
p {
margin-bottom: 16px;
}
}
/* 全屏模式样式 */
:fullscreen .video-container {
background: #000;
padding: 0;
}
:fullscreen .video-element {
width: 100%;
height: 100%;
}
/* 响应式设计 */
@media (max-width: 768px) {
.video-toolbar {
flex-direction: column;
align-items: flex-start;
padding: 8px;
}
.toolbar-left {
width: 100%;
flex-wrap: wrap;
}
.toolbar-right {
width: 100%;
justify-content: flex-start;
}
.file-info {
width: 100%;
text-align: left;
margin: 8px 0;
}
.control-buttons {
flex-direction: column;
gap: 10px;
}
.left-controls,
.right-controls {
width: 100%;
justify-content: space-between;
}
}
</style>