-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathLive.vue
More file actions
862 lines (758 loc) · 20.4 KB
/
Live.vue
File metadata and controls
862 lines (758 loc) · 20.4 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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
<template>
<div class="live-container">
<!-- 简化的标题显示 -->
<div class="simple-header">
<span class="navigation-title">Live</span>
<div class="header-actions">
<a-button
type="text"
@click="refreshData"
:loading="loading"
size="small"
>
<template #icon>
<icon-refresh />
</template>
刷新
</a-button>
<a-input-search
v-model="searchKeyword"
placeholder="搜索频道..."
style="width: 200px"
size="small"
@search="handleSearch"
@clear="handleSearchClear"
allow-clear
/>
</div>
</div>
<!-- 主要内容区域 -->
<div class="live-content">
<!-- 加载状态 -->
<div v-if="loading && !liveData" class="loading-container">
<a-spin :size="32" tip="正在加载直播数据..." />
</div>
<!-- 错误状态 -->
<div v-else-if="error" class="error-container">
<a-result
status="error"
:title="error"
sub-title="请检查网络连接或直播配置"
>
<template #extra>
<a-button type="primary" @click="refreshData">
重新加载
</a-button>
<a-button @click="goToSettings">
检查设置
</a-button>
</template>
</a-result>
</div>
<!-- 无配置状态 -->
<div v-else-if="!hasLiveConfig" class="no-config-container">
<a-result
status="info"
title="未配置直播源"
sub-title="请先在设置页面配置直播地址"
>
<template #extra>
<a-button type="primary" @click="goToSettings">
前往设置
</a-button>
</template>
</a-result>
</div>
<!-- 直播内容 -->
<div v-else-if="liveData" class="live-main">
<!-- 左侧分组列表 -->
<div class="groups-panel">
<div class="panel-header">
<h3>分组列表</h3>
<span class="group-count">{{ filteredGroups.length }}个分组</span>
</div>
<div class="groups-list">
<div
v-for="group in filteredGroups"
:key="group.name"
class="group-item"
:class="{ active: selectedGroup === group.name }"
@click="selectGroup(group.name)"
>
<div class="group-info">
<span class="group-name">{{ group.name }}</span>
<span class="channel-count">{{ group.channels.length }}</span>
</div>
</div>
</div>
</div>
<!-- 中间频道列表 -->
<div class="channels-panel">
<div class="panel-header">
<h3>频道列表</h3>
<span class="channel-count">{{ currentChannels.length }}个频道</span>
</div>
<div class="channels-list">
<div
v-for="channel in currentChannels"
:key="channel.name"
class="channel-item"
:class="{ active: selectedChannel?.name === channel.name }"
@click="selectChannel(channel)"
>
<div class="channel-logo">
<img
v-if="channel.logo"
:src="channel.logo"
:alt="channel.name"
@error="handleImageError"
/>
<icon-live-broadcast v-else class="default-logo" />
</div>
<div class="channel-info">
<div class="channel-name">{{ channel.name }}</div>
<div class="channel-group">{{ channel.group }}</div>
</div>
</div>
</div>
</div>
<!-- 右侧播放器预览区域 -->
<div class="player-panel">
<div class="panel-header">
<h3>播放预览</h3>
<div class="player-controls" v-if="selectedChannel">
<a-button
type="text"
size="small"
@click="copyChannelUrl"
>
<template #icon>
<icon-copy />
</template>
复制链接
</a-button>
<a-button
type="text"
size="small"
@click="openInNewWindow"
>
<template #icon>
<icon-launch />
</template>
新窗口播放
</a-button>
</div>
</div>
<div class="player-content">
<div v-if="!selectedChannel" class="no-selection">
<icon-play-circle class="no-selection-icon" />
<p>请选择一个频道开始播放</p>
</div>
<div v-else class="player-wrapper">
<div class="channel-header">
<div class="channel-title">
<h4>{{ selectedChannel.name }}</h4>
<span class="channel-url">{{ getCurrentChannelUrl() }}</span>
</div>
<!-- 线路切换器 -->
<div v-if="selectedChannel.routes && selectedChannel.routes.length > 1" class="route-switcher">
<span class="route-label">线路选择:</span>
<a-radio-group
v-model:value="currentRouteId"
@change="switchRoute"
size="small"
class="route-radio-group"
:key="selectedChannel.name + '-routes'"
>
<a-radio
v-for="route in selectedChannel.routes"
:key="route.id"
:value="route.id"
class="route-radio"
>
{{ route.name }}
</a-radio>
</a-radio-group>
</div>
</div>
<!-- 简单的视频播放器 -->
<div class="video-container">
<video
ref="videoPlayer"
:src="getCurrentChannelUrl()"
controls
preload="metadata"
@error="handleVideoError"
@loadstart="handleVideoLoadStart"
@loadeddata="handleVideoLoaded"
>
您的浏览器不支持视频播放
</video>
<!-- 播放状态覆盖层 -->
<div v-if="videoLoading" class="video-loading">
<a-spin :size="32" tip="正在加载视频..." />
</div>
<div v-if="videoError" class="video-error">
<icon-exclamation-circle class="error-icon" />
<p>视频加载失败</p>
<p class="error-detail">{{ videoError }}</p>
<a-button @click="retryVideo">重试</a-button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</template>
<script setup>
import { ref, reactive, computed, onMounted, watch, nextTick, onUnmounted } from 'vue'
import mpegts from 'mpegts.js'
import { useRouter } from 'vue-router'
import { Message } from '@arco-design/web-vue'
import {
IconRefresh,
IconLiveBroadcast,
IconPlayCircle,
IconCopy,
IconLaunch,
IconExclamationCircle
} from '@arco-design/web-vue/es/icon'
import liveService from '@/api/services/live.js'
const router = useRouter()
// 响应式数据
let mpegtsPlayer = null
const loading = ref(false)
const error = ref('')
const liveData = ref(null)
const hasLiveConfig = ref(true)
const searchKeyword = ref('')
const selectedGroup = ref('')
const selectedChannel = ref(null)
const currentRouteId = ref(1) // 当前选中的线路ID
const videoLoading = ref(false)
const videoError = ref('')
const videoPlayer = ref(null)
// 计算属性
const filteredGroups = computed(() => {
if (!liveData.value) return []
let groups = liveData.value.groups
// 如果有搜索关键词,过滤分组
if (searchKeyword.value) {
const keyword = searchKeyword.value.toLowerCase()
groups = groups.filter(group =>
group.name.toLowerCase().includes(keyword) ||
group.channels.some(channel =>
channel.name.toLowerCase().includes(keyword)
)
)
}
return groups
})
const currentChannels = computed(() => {
if (!liveData.value || !selectedGroup.value) return []
const group = filteredGroups.value.find(g => g.name === selectedGroup.value)
if (!group) return []
let channels = group.channels
// 如果有搜索关键词,过滤频道
if (searchKeyword.value) {
const keyword = searchKeyword.value.toLowerCase()
channels = channels.filter(channel =>
channel.name.toLowerCase().includes(keyword)
)
}
return channels
})
// 方法
const loadLiveData = async (forceRefresh = false) => {
loading.value = true
error.value = ''
try {
// 检查是否有直播配置
const config = await liveService.getLiveConfig()
if (!config) {
hasLiveConfig.value = false
return
}
hasLiveConfig.value = true
// 获取直播数据
const data = await liveService.getLiveData(forceRefresh)
liveData.value = data
// 自动选择第一个分组
if (data.groups.length > 0 && !selectedGroup.value) {
selectedGroup.value = data.groups[0].name
}
console.log('直播数据加载成功:', data)
} catch (err) {
console.error('加载直播数据失败:', err)
error.value = err.message || '加载直播数据失败'
} finally {
loading.value = false
}
}
const refreshData = () => {
loadLiveData(true)
}
const selectGroup = (groupName) => {
selectedGroup.value = groupName
selectedChannel.value = null // 清除选中的频道
}
const selectChannel = (channel) => {
selectedChannel.value = channel
videoError.value = ''
// 使用nextTick确保DOM更新后再设置线路ID
nextTick(() => {
if (channel && channel.routes && channel.routes.length > 0) {
// 智能选择线路ID:使用第一个线路的实际ID
currentRouteId.value = Number(channel.routes[0].id)
} else {
currentRouteId.value = 1
}
// 重置视频播放器
if (videoPlayer.value) {
videoPlayer.value.load()
}
setupMpegtsPlayer()
})
}
// 获取当前频道的播放URL
function setupMpegtsPlayer() {
// 销毁旧播放器
if (mpegtsPlayer) {
mpegtsPlayer.destroy()
mpegtsPlayer = null
}
const url = getCurrentChannelUrl()
if (!url || !videoPlayer.value) return
// 判断是否为 mpegts 流(简单判断 .ts 或 mpegts 协议)
if (url.endsWith('.ts') || url.includes('mpegts') || url.includes('udpxy') ||
url.includes('/udp/') || url.includes('rtp://') || url.includes('udp://')) {
if (mpegts.isSupported()) {
mpegtsPlayer = mpegts.createPlayer({
type: 'mpegts',
url
})
mpegtsPlayer.attachMediaElement(videoPlayer.value)
mpegtsPlayer.load()
mpegtsPlayer.play()
}
}
}
onUnmounted(() => {
if (mpegtsPlayer) {
mpegtsPlayer.destroy()
mpegtsPlayer = null
}
})
const getCurrentChannelUrl = () => {
if (!selectedChannel.value) return ''
if (selectedChannel.value.routes && selectedChannel.value.routes.length > 0) {
const currentRoute = selectedChannel.value.routes.find(route => route.id === currentRouteId.value)
return currentRoute ? currentRoute.url : selectedChannel.value.routes[0].url
}
return selectedChannel.value.url || ''
}
// 切换线路
const switchRoute = (event) => {
const routeId = Number(event.target ? event.target.value : event)
if (!selectedChannel.value || !selectedChannel.value.routes) return
const route = selectedChannel.value.routes.find(r => r.id === routeId)
if (route) {
currentRouteId.value = routeId
videoError.value = ''
nextTick(() => {
if (videoPlayer.value) {
videoPlayer.value.src = route.url
videoPlayer.value.load()
}
setupMpegtsPlayer()
})
Message.success(`已切换到${route.name}`)
}
}
const handleSearch = (value) => {
searchKeyword.value = value
// 如果搜索后当前分组不在过滤结果中,选择第一个可用分组
if (filteredGroups.value.length > 0 &&
!filteredGroups.value.find(g => g.name === selectedGroup.value)) {
selectedGroup.value = filteredGroups.value[0].name
}
}
const handleSearchClear = () => {
searchKeyword.value = ''
}
const goToSettings = () => {
router.push('/settings')
}
const copyChannelUrl = async () => {
if (!selectedChannel.value) return
try {
await navigator.clipboard.writeText(selectedChannel.value.url)
Message.success('频道链接已复制到剪贴板')
} catch (err) {
console.error('复制失败:', err)
Message.error('复制失败')
}
}
const openInNewWindow = () => {
if (!selectedChannel.value) return
window.open(selectedChannel.value.url, '_blank')
}
const handleImageError = (event) => {
// 图片加载失败时隐藏图片
event.target.style.display = 'none'
}
const handleVideoError = (event) => {
console.error('视频播放错误:', event)
videoError.value = '无法播放此频道,可能是网络问题或频道源不可用'
videoLoading.value = false
}
const handleVideoLoadStart = () => {
videoLoading.value = true
videoError.value = ''
}
const handleVideoLoaded = () => {
videoLoading.value = false
}
const retryVideo = () => {
if (videoPlayer.value) {
videoError.value = ''
videoPlayer.value.load()
setupMpegtsPlayer()
}
}
// 监听选中频道变化
watch(selectedChannel, (newChannel) => {
if (newChannel) {
console.log('选中频道:', newChannel.name, newChannel.url)
}
})
// 测试方法:直接加载本地M3U文件
const testLocalM3U = async () => {
try {
const response = await fetch('/json/tv.m3u')
const content = await response.text()
// 动态导入live.js并创建实例
const { default: LiveService } = await import('@/api/services/live.js')
const liveService = new LiveService()
// 解析M3U内容
const parsedData = liveService.parseM3U(content, {})
// 设置数据
liveData.value = parsedData
// 自动选择第一个分组
if (parsedData.groups && parsedData.groups.length > 0) {
selectedGroup.value = parsedData.groups[0].name
}
} catch (error) {
console.error('加载本地M3U文件失败:', error)
}
}
// 组件挂载时加载数据
onMounted(() => {
// 先尝试正常加载,如果失败则测试本地M3U
loadLiveData().then(() => {
if (!liveData.value) {
console.log('正常加载失败,尝试测试本地M3U文件')
testLocalM3U()
}
})
})
</script>
<style scoped>
.live-container {
height: 100%;
display: flex;
flex-direction: column;
overflow: hidden;
}
.simple-header {
display: flex;
align-items: center;
justify-content: space-between;
width: 100%;
padding: 16px 20px;
background: var(--color-bg-3);
border-bottom: 1px solid var(--color-border-2);
box-sizing: border-box;
}
.navigation-title {
font-size: 16px;
font-weight: 600;
color: var(--color-text-1);
white-space: nowrap;
}
.header-actions {
display: flex;
align-items: center;
gap: 12px;
}
.live-content {
flex: 1;
padding: 16px;
overflow: hidden;
}
.loading-container,
.error-container,
.no-config-container {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.live-main {
height: 100%;
display: flex;
gap: 16px;
}
.groups-panel,
.channels-panel {
width: 280px;
background: var(--color-bg-2);
border-radius: 8px;
border: 1px solid var(--color-border-2);
display: flex;
flex-direction: column;
overflow: hidden;
}
.player-panel {
flex: 1;
background: var(--color-bg-2);
border-radius: 8px;
border: 1px solid var(--color-border-2);
display: flex;
flex-direction: column;
overflow: hidden;
min-width: 400px;
}
.panel-header {
padding: 16px;
border-bottom: 1px solid var(--color-border-2);
display: flex;
align-items: center;
justify-content: space-between;
background: var(--color-bg-3);
}
.panel-header h3 {
margin: 0;
font-size: 14px;
font-weight: 600;
color: var(--color-text-1);
}
.group-count,
.channel-count {
font-size: 12px;
color: var(--color-text-3);
}
.player-controls {
display: flex;
gap: 8px;
}
.groups-list,
.channels-list {
flex: 1;
overflow-y: auto;
padding: 8px;
}
.group-item,
.channel-item {
padding: 12px;
border-radius: 6px;
cursor: pointer;
transition: all 0.2s;
margin-bottom: 4px;
}
.group-item:hover,
.channel-item:hover {
background: var(--color-fill-2);
}
.group-item.active,
.channel-item.active {
background: var(--color-primary-light-1);
color: var(--color-primary-6);
}
.group-info {
display: flex;
align-items: center;
justify-content: space-between;
}
.group-name {
font-size: 14px;
font-weight: 500;
}
.channel-item {
display: flex;
align-items: center;
gap: 12px;
}
.channel-logo {
flex: 1;
height: 45px;
min-width: 60px;
border-radius: 6px;
overflow: hidden;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
display: flex;
align-items: center;
justify-content: center;
position: relative;
}
.channel-logo::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(255, 255, 255, 0.1);
border-radius: 6px;
}
.channel-logo img {
width: 80%;
height: 80%;
object-fit: contain;
position: relative;
z-index: 1;
filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.1));
}
.default-logo {
font-size: 20px;
color: rgba(255, 255, 255, 0.9);
position: relative;
z-index: 1;
}
.channel-info {
width: 120px;
flex-shrink: 0;
}
.channel-name {
font-size: 14px;
font-weight: 500;
color: var(--color-text-1);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.channel-group {
font-size: 12px;
color: var(--color-text-3);
margin-top: 2px;
}
.player-content {
flex: 1;
display: flex;
flex-direction: column;
overflow: hidden;
}
.no-selection {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
color: var(--color-text-3);
}
.no-selection-icon {
font-size: 48px;
margin-bottom: 16px;
}
.player-wrapper {
flex: 1;
display: flex;
flex-direction: column;
overflow: hidden;
}
.channel-header {
padding: 16px;
border-bottom: 1px solid var(--color-border-2);
background: var(--color-bg-3);
}
.channel-title h4 {
margin: 0 0 8px 0;
font-size: 16px;
color: var(--color-text-1);
}
.channel-url {
font-size: 12px;
color: var(--color-text-3);
word-break: break-all;
}
.route-switcher {
margin-top: 12px;
display: flex;
align-items: flex-start;
gap: 12px;
flex-wrap: wrap;
}
.route-label {
font-size: 12px;
color: var(--color-text-2);
white-space: nowrap;
margin-top: 4px;
}
.route-radio-group {
display: flex;
flex-wrap: wrap;
gap: 16px;
}
.route-radio {
margin-right: 0 !important;
}
.video-container {
flex: 1;
position: relative;
background: #000;
display: flex;
align-items: center;
justify-content: center;
}
.video-container video {
width: 100%;
height: 100%;
object-fit: contain;
}
.video-loading,
.video-error {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
background: rgba(0, 0, 0, 0.8);
color: white;
}
.error-icon {
font-size: 48px;
color: var(--color-danger-6);
margin-bottom: 16px;
}
.error-detail {
font-size: 12px;
color: var(--color-text-3);
margin-top: 8px;
text-align: center;
}
/* 滚动条样式 */
.groups-list::-webkit-scrollbar,
.channels-list::-webkit-scrollbar {
width: 6px;
}
.groups-list::-webkit-scrollbar-track,
.channels-list::-webkit-scrollbar-track {
background: var(--color-fill-2);
border-radius: 3px;
}
.groups-list::-webkit-scrollbar-thumb,
.channels-list::-webkit-scrollbar-thumb {
background: var(--color-fill-4);
border-radius: 3px;
}
.groups-list::-webkit-scrollbar-thumb:hover,
.channels-list::-webkit-scrollbar-thumb:hover {
background: var(--color-fill-6);
}
</style>