-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathnetease-mini-player-v2-dev.js
More file actions
2371 lines (2298 loc) · 103 KB
/
netease-mini-player-v2-dev.js
File metadata and controls
2371 lines (2298 loc) · 103 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
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* [NMPv2] NeteaseMiniPlayer v2 JavaScript
* Lightweight Player Component Based on NetEase Cloud Music API
*
* Copyright 2025 BHCN STUDIO & 北海的佰川(ImBHCN[numakkiyu])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* 播放器配置对象类型定义
* @typedef {Object} PlayerConfig
* @property {boolean} embed - 是否嵌入模式(隐藏部分控制,适配容器)
* @property {boolean} autoplay - 是否在初始化后自动播放
* @property {string | undefined} playlistId - 播放列表ID(与`songId`互斥)
* @property {string | undefined} songId - 单曲ID(与`playlistId`互斥)
* @property {('static'|'top-left'|'top-right'|'bottom-left'|'bottom-right')} position - 组件位置枚举
* @property {boolean} lyric - 是否显示歌词
* @property {('auto'|'dark'|'light')} theme - 主题模式枚举
* @property {('compact'|string)} size - 尺寸模式(默认`compact`,可按需扩展)
* @property {boolean} defaultMinimized - 是否默认最小化(需配合定位生效)
*/
/**
* 歌曲信息类型定义
* @typedef {Object} SongInfo
* @property {number | string} id - 歌曲ID
* @property {string} name - 歌曲名称
* @property {string} artists - 艺术家名称(以` / `分隔)
* @property {string} album - 专辑名称
* @property {string} picUrl - 封面图片URL
* @property {number} duration - 时长(毫秒)
*/
/**
* 歌词条目类型定义
* @typedef {Object} LyricEntry
* @property {number} time - 时间戳(秒)
* @property {string} text - 原文歌词
* @property {string} translation - 翻译歌词
*/
/**
* 运行环境信息类型定义
* @typedef {Object} UAInfo
* @property {boolean} isMobile - 是否移动端环境
* @property {boolean} isiOS - 是否iOS设备
* @property {boolean} isAndroid - 是否Android设备
* @property {boolean} isHarmonyOS - 是否HarmonyOS设备
* @property {boolean} isHarmonyDesktop - 是否HarmonyOS桌面环境
* @property {boolean} isWeChat - 是否微信WebView
* @property {boolean} isQQ - 是否QQ或MQQBrowser
* @property {boolean} isInAppWebView - 是否App内置WebView
* @property {boolean} isPWA - 是否PWA独立模式
* @property {boolean} isiPad - 是否iPad或iPad风格设备
*/
/**
* DOM引用映射类型定义
* @typedef {Object.<string, HTMLElement>} ElementsMap
*/
/**
* 全局音频管理器,确保同一时间仅有一个播放器实例在播放
* @namespace GlobalAudioManager
* @property {NeteaseMiniPlayer | null} currentPlayer - 当前激活的播放器实例
* @example
* // 设置新的当前播放器,会自动暂停之前的播放器
* GlobalAudioManager.setCurrent(player);
*/
const GlobalAudioManager = {
currentPlayer: null,
/**
* 设置当前播放器实例,并在必要时暂停之前的实例
* @param {NeteaseMiniPlayer} player - 需要设置为当前的播放器实例
*/
setCurrent(player) {
if (this.currentPlayer && this.currentPlayer !== player) {
this.currentPlayer.pause();
}
this.currentPlayer = player;
}
};
const ICONS = {
prev: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640"><path d="M556.2 541.6C544.2 546.6 530.5 543.8 521.3 534.7L352 365.3L352 512C352 524.9 344.2 536.6 332.2 541.6C320.2 546.6 306.5 543.8 297.3 534.7L128 365.3L128 512C128 529.7 113.7 544 96 544C78.3 544 64 529.7 64 512L64 128C64 110.3 78.3 96 96 96C113.7 96 128 110.3 128 128L128 274.7L297.4 105.4C306.6 96.2 320.3 93.5 332.3 98.5C344.3 103.5 352 115.1 352 128L352 274.7L521.4 105.3C530.6 96.1 544.3 93.4 556.3 98.4C568.3 103.4 576 115.1 576 128L576 512C576 524.9 568.2 536.6 556.2 541.6z"/></svg>`,
next: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640"><path d="M83.8 541.6C95.8 546.6 109.5 543.8 118.7 534.7L288 365.3L288 512C288 524.9 295.8 536.6 307.8 541.6C319.8 546.6 333.5 543.8 342.7 534.7L512 365.3L512 512C512 529.7 526.3 544 544 544C561.7 544 576 529.7 576 512L576 128C576 110.3 561.7 96 544 96C526.3 96 512 110.3 512 128L512 274.7L342.6 105.3C333.4 96.1 319.7 93.4 307.7 98.4C295.7 103.4 288 115.1 288 128L288 274.7L118.6 105.4C109.4 96.2 95.7 93.5 83.7 98.5C71.7 103.5 64 115.1 64 128L64 512C64 524.9 71.8 536.6 83.8 541.6z"/></svg>`,
play: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640"><path d="M187.2 100.9C174.8 94.1 159.8 94.4 147.6 101.6C135.4 108.8 128 121.9 128 136L128 504C128 518.1 135.5 531.2 147.6 538.4C159.7 545.6 174.8 545.9 187.2 539.1L523.2 355.1C536 348.1 544 334.6 544 320C544 305.4 536 291.9 523.2 284.9L187.2 100.9z"/></svg>`,
pause: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640"><path d="M176 96C149.5 96 128 117.5 128 144L128 496C128 522.5 149.5 544 176 544L240 544C266.5 544 288 522.5 288 496L288 144C288 117.5 266.5 96 240 96L176 96zM400 96C373.5 96 352 117.5 352 144L352 496C352 522.5 373.5 544 400 544L464 544C490.5 544 512 522.5 512 496L512 144C512 117.5 490.5 96 464 96L400 96z"/></svg>`,
volume: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640"><path d="M533.6 96.5C523.3 88.1 508.2 89.7 499.8 100C491.4 110.3 493 125.4 503.3 133.8C557.5 177.8 592 244.8 592 320C592 395.2 557.5 462.2 503.3 506.3C493 514.7 491.5 529.8 499.8 540.1C508.1 550.4 523.3 551.9 533.6 543.6C598.5 490.7 640 410.2 640 320C640 229.8 598.5 149.2 533.6 96.5zM473.1 171C462.8 162.6 447.7 164.2 439.3 174.5C430.9 184.8 432.5 199.9 442.8 208.3C475.3 234.7 496 274.9 496 320C496 365.1 475.3 405.3 442.8 431.8C432.5 440.2 431 455.3 439.3 465.6C447.6 475.9 462.8 477.4 473.1 469.1C516.3 433.9 544 380.2 544 320.1C544 260 516.3 206.3 473.1 171.1zM412.6 245.5C402.3 237.1 387.2 238.7 378.8 249C370.4 259.3 372 274.4 382.3 282.8C393.1 291.6 400 305 400 320C400 335 393.1 348.4 382.3 357.3C372 365.7 370.5 380.8 378.8 391.1C387.1 401.4 402.3 402.9 412.6 394.6C434.1 376.9 448 350.1 448 320C448 289.9 434.1 263.1 412.6 245.5zM80 416L128 416L262.1 535.2C268.5 540.9 276.7 544 285.2 544C304.4 544 320 528.4 320 509.2L320 130.8C320 111.6 304.4 96 285.2 96C276.7 96 268.5 99.1 262.1 104.8L128 224L80 224C53.5 224 32 245.5 32 272L32 368C32 394.5 53.5 416 80 416z"/></svg>`,
lyrics: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640"><path d="M532 71C539.6 77.1 544 86.3 544 96L544 400C544 444.2 501 480 448 480C395 480 352 444.2 352 400C352 355.8 395 320 448 320C459.2 320 470 321.6 480 324.6L480 207.9L256 257.7L256 464C256 508.2 213 544 160 544C107 544 64 508.2 64 464C64 419.8 107 384 160 384C171.2 384 182 385.6 192 388.6L192 160C192 145 202.4 132 217.1 128.8L505.1 64.8C514.6 62.7 524.5 65 532.1 71.1z"/></svg>`,
list: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640"><path d="M104 112C90.7 112 80 122.7 80 136L80 184C80 197.3 90.7 208 104 208L152 208C165.3 208 176 197.3 176 184L176 136C176 122.7 165.3 112 152 112L104 112zM256 128C238.3 128 224 142.3 224 160C224 177.7 238.3 192 256 192L544 192C561.7 192 576 177.7 576 160C576 142.3 561.7 128 544 128L256 128zM256 288C238.3 288 224 302.3 224 320C224 337.7 238.3 352 256 352L544 352C561.7 352 576 337.7 576 320C576 302.3 561.7 288 544 288L256 288zM256 448C238.3 448 224 462.3 224 480C224 497.7 238.3 512 256 512L544 512C561.7 512 576 497.7 576 480C576 462.3 561.7 448 544 448L256 448zM80 296L80 344C80 357.3 90.7 368 104 368L152 368C165.3 368 176 357.3 176 344L176 296C176 282.7 165.3 272 152 272L104 272C90.7 272 80 282.7 80 296zM104 432C90.7 432 80 442.7 80 456L80 504C80 517.3 90.7 528 104 528L152 528C165.3 528 176 517.3 176 504L176 456C176 442.7 165.3 432 152 432L104 432z"/></svg>`,
minimize: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640"><path d="M64 320C64 178.6 178.6 64 320 64C461.4 64 576 178.6 576 320C576 461.4 461.4 576 320 576C178.6 576 64 461.4 64 320zM320 352C302.3 352 288 337.7 288 320C288 302.3 302.3 288 320 288C337.7 288 352 302.3 352 320C352 337.7 337.7 352 320 352zM224 320C224 373 267 416 320 416C373 416 416 373 416 320C416 267 373 224 320 224C267 224 224 267 224 320zM168 304C168 271.6 184.3 237.4 210.8 210.8C237.3 184.2 271.6 168 304 168C317.3 168 328 157.3 328 144C328 130.7 317.3 120 304 120C256.1 120 210.3 143.5 176.9 176.9C143.5 210.3 120 256.1 120 304C120 317.3 130.7 328 144 328C157.3 328 168 317.3 168 304z"/></svg>`,
maximize: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640"><path d="M64 320C64 178.6 178.6 64 320 64C461.4 64 576 178.6 576 320C576 461.4 461.4 576 320 576C178.6 576 64 461.4 64 320zM320 352C302.3 352 288 337.7 288 320C288 302.3 302.3 288 320 288C337.7 288 352 302.3 352 320C352 337.7 337.7 352 320 352zM224 320C224 373 267 416 320 416C373 416 416 373 416 320C416 267 373 224 320 224C267 224 224 267 224 320zM168 304C168 271.6 184.3 237.4 210.8 210.8C237.3 184.2 271.6 168 304 168C317.3 168 328 157.3 328 144C328 130.7 317.3 120 304 120C256.1 120 210.3 143.5 176.9 176.9C143.5 210.3 120 256.1 120 304C120 317.3 130.7 328 144 328C157.3 328 168 317.3 168 304z"/></svg>`,
loopList: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640"><path d="M534.6 182.6C547.1 170.1 547.1 149.8 534.6 137.3L470.6 73.3C461.4 64.1 447.7 61.4 435.7 66.4C423.7 71.4 416 83.1 416 96L416 128L256 128C150 128 64 214 64 320C64 337.7 78.3 352 96 352C113.7 352 128 337.7 128 320C128 249.3 185.3 192 256 192L416 192L416 224C416 236.9 423.8 248.6 435.8 253.6C447.8 258.6 461.5 255.8 470.7 246.7L534.7 182.7zM105.4 457.4C92.9 469.9 92.9 490.2 105.4 502.7L169.4 566.7C178.6 575.9 192.3 578.6 204.3 573.6C216.3 568.6 224 556.9 224 544L224 512L384 512C490 512 576 426 576 320C576 302.3 561.7 288 544 288C526.3 288 512 302.3 512 320C512 390.7 454.7 448 384 448L224 448L224 416C224 403.1 216.2 391.4 204.2 386.4C192.2 381.4 178.5 384.2 169.3 393.3L105.3 457.3z"/></svg>`,
loopSingle: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640"><path d="M534.6 182.6C547.1 170.1 547.1 149.8 534.6 137.3L470.6 73.3C461.4 64.1 447.7 61.4 435.7 66.4C423.7 71.4 416 83.1 416 96L416 128L256 128C150 128 64 214 64 320C64 337.7 78.3 352 96 352C113.7 352 128 337.7 128 320C128 249.3 185.3 192 256 192L416 192L416 224C416 236.9 423.8 248.6 435.8 253.6C447.8 258.6 461.5 255.8 470.7 246.7L534.7 182.7zM105.4 457.4C92.9 469.9 92.9 490.2 105.4 502.7L169.4 566.7C178.6 575.9 192.3 578.6 204.3 573.6C216.3 568.6 224 556.9 224 544L224 512L384 512C490 512 576 426 576 320C576 302.3 561.7 288 544 288C526.3 288 512 302.3 512 320C512 390.7 454.7 448 384 448L224 448L224 416C224 403.1 216.2 391.4 204.2 386.4C192.2 381.4 178.5 384.2 169.3 393.3L105.3 457.3z"/><path d="M295 280L305 260L335 260L335 380L305 380L305 280Z"/></svg>`,
shuffle: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640"><path d="M467.8 98.4C479.8 93.4 493.5 96.2 502.7 105.3L566.7 169.3C572.7 175.3 576.1 183.4 576.1 191.9C576.1 200.4 572.7 208.5 566.7 214.5L502.7 278.5C493.5 287.7 479.8 290.4 467.8 285.4C455.8 280.4 448 268.9 448 256L448 224L416 224C405.9 224 396.4 228.7 390.4 236.8L358 280L318 226.7L339.2 198.4C357.3 174.2 385.8 160 416 160L448 160L448 128C448 115.1 455.8 103.4 467.8 98.4zM218 360L258 413.3L236.8 441.6C218.7 465.8 190.2 480 160 480L96 480C78.3 480 64 465.7 64 448C64 430.3 78.3 416 96 416L160 416C170.1 416 179.6 411.3 185.6 403.2L218 360zM502.6 534.6C493.4 543.8 479.7 546.5 467.7 541.5C455.7 536.5 448 524.9 448 512L448 480L416 480C385.8 480 357.3 465.8 339.2 441.6L185.6 236.8C179.6 228.7 170.1 224 160 224L96 224C78.3 224 64 209.7 64 192C64 174.3 78.3 160 96 160L160 160C190.2 160 218.7 174.2 236.8 198.4L390.4 403.2C396.4 411.3 405.9 416 416 416L448 416L448 384C448 371.1 455.8 359.4 467.8 354.4C479.8 349.4 493.5 352.2 502.7 361.3L566.7 425.3C572.7 431.3 576.1 439.4 576.1 447.9C576.1 456.4 572.7 464.5 566.7 470.5L502.7 534.5z"/></svg>`
};
/**
* 播放器核心类,管理音频播放、状态同步、歌词解析和UI更新
* @class NeteaseMiniPlayer
* @constructor
* @description
* - 适用于网页嵌入式或独立悬浮播放控件
* - 支持歌单/单曲加载、歌词解析、主题跟随与移动端优化
* - 注意:同一页面建议使用全局音频管理器避免多实例同时播放
* @see https://github.com/numakkiyu/NeteaseMiniPlayer
* @property {HTMLElement} element - 容器元素(组件挂载目标)
* @property {PlayerConfig} config - 播放器配置项
* @property {SongInfo | null} currentSong - 当前歌曲信息
* @property {SongInfo[]} playlist - 播放列表(按索引顺序播放)
* @property {number} currentIndex - 当前播放索引
* @property {HTMLAudioElement} audio - 原生Audio实例
* @property {boolean} wasPlayingBeforeHidden - 页面隐藏前是否在播放
* @property {boolean} isPlaying - 是否正在播放
* @property {number} currentTime - 当前播放进度(秒)
* @property {number} duration - 当前歌曲总时长(秒)
* @property {number} volume - 音量值(0.0 - 1.0)
* @property {LyricEntry[]} lyrics - 已解析的歌词列表
* @property {number} currentLyricIndex - 当前歌词行索引
* @property {boolean} showLyrics - 是否显示歌词
* @property {Map<string, any>} cache - 简易缓存(带过期时间)
* @property {('list'|'single'|'shuffle')} playMode - 播放模式枚举
* @property {number[]} shuffleHistory - 随机播放历史索引(避免连续重复)
* @property {number | null} idleTimeout - 空闲淡出定时器ID
* @property {number} idleDelay - 空闲淡出延迟(毫秒)
* @property {boolean} isIdle - 是否处于空闲淡出态
* @property {boolean} isMinimized - 是否处于最小化态
* @property {ElementsMap} elements - 关键DOM引用映射
*
* @example
* const el = document.querySelector('.netease-mini-player');
* const player = new NeteaseMiniPlayer(el);
* // 加载并播放播放列表
* player.parseConfig();
* player.init().then(() => player.play());
*/
class NeteaseMiniPlayer {
/**
* 构造函数,初始化实例并解析配置
* @param {HTMLElement} element - 组件挂载的容器元素
*/
constructor(element) {
this.element = element;
this.element.neteasePlayer = this;
this.config = this.parseConfig();
this.currentSong = null;
this.playlist = [];
this.currentIndex = 0;
this.audio = new Audio();
this.wasPlayingBeforeHidden = false;
this.isPlaying = false;
this.currentTime = 0;
this.duration = 0;
this.volume = 0.7;
this.lyrics = [];
this.currentLyricIndex = -1;
this.showLyrics = this.config.lyric;
this.cache = new Map();
this.init();
this.playMode = 'list';
this.shuffleHistory = [];
this.idleTimeout = null;
this.idleDelay = 5000;
this.isIdle = false;
}
/**
* 解析容器上的`data-*`属性为内部配置
* @returns {PlayerConfig} 返回解析后的配置对象
*/
parseConfig() {
const element = this.element;
const position = element.dataset.position || 'static';
const validPositions = ['static', 'top-left', 'top-right', 'bottom-left', 'bottom-right'];
const finalPosition = validPositions.includes(position) ? position : 'static';
const defaultMinimized = element.dataset.defaultMinimized === 'true';
const embedValue = element.getAttribute('data-embed') || element.dataset.embed;
const isEmbed = embedValue === 'true' || embedValue === true;
const autoPauseAttr = element.getAttribute('data-auto-pause') ?? element.dataset.autoPause;
const autoPauseDisabled = autoPauseAttr === 'true' || autoPauseAttr === true;
return {
embed: isEmbed,
autoplay: element.dataset.autoplay === 'true',
playlistId: element.dataset.playlistId,
songId: element.dataset.songId,
position: finalPosition,
lyric: element.dataset.lyric !== 'false',
theme: element.dataset.theme || 'auto',
size: element.dataset.size || 'compact',
defaultMinimized: defaultMinimized,
autoPauseDisabled: autoPauseDisabled
};
}
/**
* 初始化组件:主题、结构、事件绑定与数据加载
* @returns {Promise<void>} 异步初始化完成
* @throws {Error} 当数据加载失败或DOM不可用时抛出异常
*/
async init() {
if (this.config.embed) {
this.element.setAttribute('data-embed', 'true');
}
this.element.setAttribute('data-position', this.config.position);
if (this.config.embed) {
this.element.classList.add('netease-mini-player-embed');
}
this.initTheme();
this.createPlayerHTML();
this.applyResponsiveControls?.();
this.setupEnvListeners?.();
this.bindEvents();
this.setupAudioEvents();
try {
if (this.config.embed) {
if (this.config.songId) {
await this.loadSingleSong(this.config.songId);
} else if (this.config.playlistId) {
await this.loadPlaylist(this.config.playlistId);
this.playlist = [this.playlist[0]];
}
} else {
if (this.config.playlistId) {
await this.loadPlaylist(this.config.playlistId);
} else if (this.config.songId) {
await this.loadSingleSong(this.config.songId);
}
}
if (this.playlist.length > 0) {
await this.loadCurrentSong();
if (this.config.autoplay && !this.config.embed) {
// 静音自动播放策略(Silent Warm-up)
// 1. 先静音启动
this.audio.muted = true;
// 双重保险:音量先设为0
const originalVolume = this.volume;
this.audio.volume = 0;
try {
await this.play();
} catch (e) {
console.log('静音自动播放被拦截,转为交互后播放');
}
// 2. 监听所有可能的用户交互事件来恢复音量
const interactionEvents = ['click', 'touchstart', 'keydown', 'wheel', 'scroll'];
const enableAudio = () => {
// 移除所有监听器
interactionEvents.forEach(event => {
document.removeEventListener(event, enableAudio);
});
// 恢复音量设置(淡入效果)
this.audio.muted = false;
// 如果之前播放失败了(不在播放状态),则尝试再次播放
if (!this.isPlaying) {
this.play().then(() => {
// 播放成功后开始淡入
this.audio.volume = 0;
fadeIn();
}).catch(e => console.error('交互后播放失败:', e));
} else {
// 已经在静音播放中,直接淡入
this.audio.volume = 0;
fadeIn();
}
// 淡入动画函数
const fadeIn = () => {
let currentVol = 0;
const targetVol = originalVolume;
const step = targetVol / 10; // 分10步完成
const interval = 50; // 每50ms一步,共500ms
const fadeTimer = setInterval(() => {
currentVol += step;
if (currentVol >= targetVol) {
currentVol = targetVol;
clearInterval(fadeTimer);
}
this.audio.volume = currentVol;
this.volume = currentVol; // 同步内部状态
}, interval);
};
};
if (this.isPlaying) {
// 如果静音播放成功,只需要等待交互恢复音量
interactionEvents.forEach(event => {
document.addEventListener(event, enableAudio, { once: true, passive: true });
});
} else {
// 如果静音播放也失败了,说明策略严格,必须等待交互才能开始播放
this.audio.muted = false; // 恢复状态,等待交互
this.audio.volume = originalVolume;
interactionEvents.forEach(event => {
document.addEventListener(event, enableAudio, { once: true, passive: true });
});
}
}
}
if (this.config.defaultMinimized && !this.config.embed && this.config.position !== 'static') {
this.toggleMinimize();
}
} catch (error) {
console.error('播放器初始化失败:', error);
this.showError('加载失败,请稍后重试');
}
}
/**
* 创建播放器DOM结构并抓取关键引用
* @returns {void}
*/
createPlayerHTML() {
this.element.innerHTML = `
<div class="player-main">
<div class="album-cover-container">
<img class="album-cover" src="" alt="专辑封面">
<div class="vinyl-overlay">
<div class="vinyl-center"></div>
</div>
</div>
<div class="song-content">
<div class="song-info">
<div class="song-title">加载中...</div>
<div class="song-artist">请稍候</div>
</div>
<div class="lyrics-container">
<div class="lyric-line original">♪ 加载歌词中... ♪</div>
<div class="lyric-line translation"></div>
</div>
</div>
<div class="controls">
${!this.config.embed ? `<button class="control-btn prev-btn" title="上一首">${ICONS.prev}</button>` : ''}
<button class="control-btn play-btn" title="播放/暂停">
<span class="play-icon">${ICONS.play}</span>
<span class="pause-icon" style="display: none;">${ICONS.pause}</span>
</button>
${!this.config.embed ? `<button class="control-btn next-btn" title="下一首">${ICONS.next}</button>` : ''}
</div>
</div>
<div class="player-bottom">
<div class="progress-container">
<span class="time-display current-time">0:00</span>
<div class="progress-bar-container">
<div class="progress-bar"></div>
</div>
<span class="time-display total-time">0:00</span>
</div>
<div class="bottom-controls">
<div class="volume-container">
<span class="volume-icon">${ICONS.volume}</span>
<div class="volume-slider-container">
<div class="volume-slider">
<div class="volume-bar"></div>
</div>
</div>
</div>
<span class="feature-btn lyrics-btn" role="button" title="显示/隐藏歌词">${ICONS.lyrics}</span>
${!this.config.embed ? `<span class="feature-btn loop-mode-btn" role="button" title="列表循环">${ICONS.loopList}</span>` : ''}
${!this.config.embed ? `<span class="feature-btn list-btn" role="button" title="播放列表">${ICONS.list}</span>` : ''}
${!this.config.embed ? `<span class="feature-btn minimize-btn" role="button" title="缩小/展开">${ICONS.minimize}</span>` : ''}
</div>
</div>
<div class="playlist-container">
<div class="playlist-content"></div>
</div>
`;
this.elements = {
albumCover: this.element.querySelector('.album-cover'),
albumCoverContainer: this.element.querySelector('.album-cover-container'),
songTitle: this.element.querySelector('.song-title'),
songArtist: this.element.querySelector('.song-artist'),
lyricsContainer: this.element.querySelector('.lyrics-container'),
lyricLine: this.element.querySelector('.lyric-line.original'),
lyricTranslation: this.element.querySelector('.lyric-line.translation'),
playBtn: this.element.querySelector('.play-btn'),
playIcon: this.element.querySelector('.play-icon'),
pauseIcon: this.element.querySelector('.pause-icon'),
prevBtn: this.element.querySelector('.prev-btn'),
nextBtn: this.element.querySelector('.next-btn'),
progressContainer: this.element.querySelector('.progress-bar-container'),
progressBar: this.element.querySelector('.progress-bar'),
currentTime: this.element.querySelector('.current-time'),
totalTime: this.element.querySelector('.total-time'),
volumeContainer: this.element.querySelector('.volume-container'),
volumeSlider: this.element.querySelector('.volume-slider'),
volumeBar: this.element.querySelector('.volume-bar'),
volumeIcon: this.element.querySelector('.volume-icon'),
lyricsBtn: this.element.querySelector('.lyrics-btn'),
listBtn: this.element.querySelector('.list-btn'),
minimizeBtn: this.element.querySelector('.minimize-btn'),
playlistContainer: this.element.querySelector('.playlist-container'),
playlistContent: this.element.querySelector('.playlist-content')
};
this.isMinimized = false;
this.elements.loopModeBtn = this.element.querySelector('.loop-mode-btn');
}
/**
* 绑定交互事件(播放、进度、音量、列表、最小化等)
* @returns {void}
*/
bindEvents() {
this.elements.playBtn.addEventListener('click', () => this.togglePlay());
if (this.elements.prevBtn) {
this.elements.prevBtn.addEventListener('click', () => this.previousSong());
}
if (this.elements.nextBtn) {
this.elements.nextBtn.addEventListener('click', () => this.nextSong());
}
if (this.elements.loopModeBtn) {
this.elements.loopModeBtn.addEventListener('click', () => this.togglePlayMode());
}
this.elements.albumCoverContainer.addEventListener('click', () => {
if (this.element.classList.contains('minimized')) {
this.elements.albumCoverContainer.classList.toggle('expanded');
return;
}
if (this.currentSong && this.currentSong.id) {
const songUrl = `https://music.163.com/song?id=${this.currentSong.id}`;
window.open(songUrl, '_blank', 'noopener,noreferrer');
}
});
let isDragging = false;
this.elements.progressContainer.addEventListener('mousedown', (e) => {
isDragging = true;
this.seekTo(e);
});
document.addEventListener('mousemove', (e) => {
if (isDragging) {
this.seekTo(e);
}
});
document.addEventListener('mouseup', () => {
isDragging = false;
});
this.elements.progressContainer.addEventListener('click', (e) => this.seekTo(e));
let isVolumesDragging = false;
this.elements.volumeSlider.addEventListener('mousedown', (e) => {
isVolumesDragging = true;
this.setVolume(e);
});
document.addEventListener('mousemove', (e) => {
if (isVolumesDragging) {
this.setVolume(e);
}
});
document.addEventListener('mouseup', () => {
isVolumesDragging = false;
});
this.elements.volumeSlider.addEventListener('click', (e) => this.setVolume(e));
this.elements.lyricsBtn.addEventListener('click', () => this.toggleLyrics());
if (this.elements.listBtn) {
this.elements.listBtn.addEventListener('click', () => this.togglePlaylist());
}
if (this.elements.minimizeBtn) {
this.elements.minimizeBtn.addEventListener('click', () => this.toggleMinimize());
}
document.addEventListener('click', (e) => {
if (this.elements.playlistContainer &&
this.elements.playlistContainer.classList.contains('show')) {
if (!this.element.contains(e.target)) {
this.togglePlaylist(false);
}
}
});
if (this.config.position !== 'static' && !this.config.embed) {
this.setupDragAndDrop();
}
if (typeof document.hidden !== 'undefined') {
document.addEventListener('visibilitychange', () => {
if (document.hidden && this.isPlaying) {
this.wasPlayingBeforeHidden = true;
this.pause();
} else if (!document.hidden && this.wasPlayingBeforeHidden) {
this.play();
this.wasPlayingBeforeHidden = false;
}
});
}
this.element.addEventListener('mouseenter', () => {
this.restoreOpacity();
});
this.element.addEventListener('mouseleave', () => {
this.startIdleTimer();
});
this.applyIdlePolicyOnInit();
}
/**
* 启动空闲淡出计时器,在最小化时降低存在感
* @returns {void}
* @description
* 1. 首先清除现有的空闲计时器
* 2. 检查是否启用空闲透明度功能
* 3. 设置新的计时器,延迟后触发淡出效果
* 4. 计时器时间由idleDelay配置决定
* 5. 计时器触发时调用triggerFadeOut开始淡出动画
* @example
* player.startIdleTimer(); // 启动空闲计时器
* @private
*/
startIdleTimer() {
this.clearIdleTimer();
if (!this.shouldEnableIdleOpacity()) return;
this.idleTimeout = setTimeout(() => {
this.triggerFadeOut();
}, this.idleDelay);
}
/**
* 清除空闲淡出计时器
* @returns {void}
* @description
* 1. 检查是否存在空闲计时器实例
* 2. 使用clearTimeout清除计时器
* 3. 将计时器引用设为null,避免内存泄漏
* 4. 通常在用户交互或播放器状态改变时调用
* @example
* player.clearIdleTimer(); // 清除正在运行的空闲计时器
* @private
*/
clearIdleTimer() {
if (this.idleTimeout) {
clearTimeout(this.idleTimeout);
this.idleTimeout = null;
}
}
/**
* 触发淡出动画并根据定位进行侧边停靠
* @returns {void}
* @description
* 1. 检查是否启用空闲透明度功能
* 2. 检查是否已经在空闲状态,避免重复触发
* 3. 移除淡入动画类名
* 4. 获取当前停靠侧边(左/右)
* 5. 添加对应的停靠类名和淡出动画类名
* 6. 监听动画结束事件,完成后切换到空闲状态
* @example
* player.triggerFadeOut(); // 开始淡出到侧边停靠状态
* @private
*/
triggerFadeOut() {
if (!this.shouldEnableIdleOpacity()) return;
if (this.isIdle) return;
this.isIdle = true;
this.element.classList.remove('fading-in');
const side = this.getDockSide();
if (side) {
this.element.classList.add(`docked-${side}`);
}
this.element.classList.add('fading-out');
const onEnd = (e) => {
if (e.animationName !== 'player-fade-out') return;
this.element.classList.remove('fading-out');
this.element.classList.add('idle');
this.element.removeEventListener('animationend', onEnd);
};
this.element.addEventListener('animationend', onEnd);
}
/**
* 恢复不透明度,移除停靠并播放弹出动画
* @returns {void}
* @description
* 1. 首先清除空闲计时器
* 2. 获取当前停靠侧边和停靠状态
* 3. 如果处于停靠状态,播放弹出动画
* 4. 监听弹出动画结束,移除停靠类名
* 5. 播放淡入动画,恢复到正常显示状态
* 6. 监听淡入动画结束,清理动画类名
* @example
* player.restoreOpacity(); // 从停靠状态恢复到正常显示
* @private
*/
restoreOpacity() {
this.clearIdleTimer();
const side = this.getDockSide();
const hasDock = side ? this.element.classList.contains(`docked-${side}`) : false;
if (hasDock) {
const popAnim = side === 'right' ? 'player-popout-right' : 'player-popout-left';
this.element.classList.add(`popping-${side}`);
const onPopEnd = (e) => {
if (e.animationName !== popAnim) return;
this.element.removeEventListener('animationend', onPopEnd);
this.element.classList.remove(`popping-${side}`);
this.element.classList.remove(`docked-${side}`);
if (this.isIdle) {
this.isIdle = false;
}
this.element.classList.remove('idle', 'fading-out');
this.element.classList.add('fading-in');
const onEndIn = (ev) => {
if (ev.animationName !== 'player-fade-in') return;
this.element.classList.remove('fading-in');
this.element.removeEventListener('animationend', onEndIn);
};
this.element.addEventListener('animationend', onEndIn);
};
this.element.addEventListener('animationend', onPopEnd);
return;
}
if (!this.isIdle) return;
this.isIdle = false;
this.element.classList.remove('idle', 'fading-out');
this.element.classList.add('fading-in');
const onEndIn = (ev) => {
if (ev.animationName !== 'player-fade-in') return;
this.element.classList.remove('fading-in');
this.element.removeEventListener('animationend', onEndIn);
};
this.element.addEventListener('animationend', onEndIn);
}
/**
* 判断是否启用空闲透明度功能
* @returns {boolean} 启用返回true
* @description
* 1. 检查播放器是否处于最小化状态
* 2. 仅当最小化时才启用空闲透明度
* 3. 用于控制空闲时的淡出效果
* @example
* player.isMinimized = true;
* player.shouldEnableIdleOpacity(); // true
*
* player.isMinimized = false;
* player.shouldEnableIdleOpacity(); // false
* @private
*/
shouldEnableIdleOpacity() {
return this.isMinimized === true;
}
/**
* 初始化时应用空闲策略设置
* @returns {void}
* @description
* 1. 检查是否启用空闲透明度功能
* 2. 如果未启用,清除空闲计时器
* 3. 重置空闲状态标识
* 4. 移除所有相关的CSS类名(停靠、动画等)
* 5. 确保播放器以正常状态显示
* @example
* player.applyIdlePolicyOnInit(); // 根据配置应用空闲策略
* @private
*/
applyIdlePolicyOnInit() {
if (!this.shouldEnableIdleOpacity()) {
this.clearIdleTimer();
this.isIdle = false;
this.element.classList.remove('idle', 'fading-in', 'fading-out', 'docked-left', 'docked-right', 'popping-left', 'popping-right');
}
}
/**
* 计算当前停靠方向
* @returns {'left'|'right'} 返回停靠侧边枚举
* @description
* 1. 根据配置中的position值判断停靠方向
* 2. 左上角和左下角返回'left'
* 3. 右上角和右下角返回'right'
* 4. 其他位置默认返回'right'
* @example
* player.config.position = 'top-left';
* player.getDockSide(); // 'left'
*
* player.config.position = 'bottom-right';
* player.getDockSide(); // 'right'
* @private
*/
getDockSide() {
const pos = this.config.position;
if (pos === 'top-left' || pos === 'bottom-left') return 'left';
if (pos === 'top-right' || pos === 'bottom-right') return 'right';
return 'right';
}
/**
* 获取运行环境信息(UA & 媒体特征)
* @returns {UAInfo} 环境信息结构
* @description
* 1. 检查是否存在缓存的环境信息,有则直接返回
* 2. 解析用户代理字符串获取平台和设备信息
* 3. 检测触摸支持、媒体播放能力等特性
* 4. 缓存结果以提高后续访问性能
* @example
* const uaInfo = NeteaseMiniPlayer.getUAInfo();
* console.log(uaInfo.platform); // 'mobile' | 'tablet' | 'desktop'
* console.log(uaInfo.canPlayType); // 是否支持音频播放
* @private
*/
static getUAInfo() {
if (NeteaseMiniPlayer._uaCache) return NeteaseMiniPlayer._uaCache;
const nav = typeof navigator !== 'undefined' ? navigator : {};
const uaRaw = (nav.userAgent || '');
const ua = uaRaw.toLowerCase();
const platform = (nav.platform || '').toLowerCase();
const maxTP = nav.maxTouchPoints || 0;
const isWeChat = /micromessenger/.test(ua);
const isQQ = /(mqqbrowser| qq)/.test(ua);
const isInAppWebView = /\bwv\b|; wv/.test(ua) || /version\/\d+.*chrome/.test(ua);
const isiPhone = /iphone/.test(ua);
const isiPadUA = /ipad/.test(ua);
const isIOSLikePad = !isiPadUA && platform.includes('mac') && maxTP > 1;
const isiOS = isiPhone || isiPadUA || isIOSLikePad;
const isAndroid = /android/.test(ua);
const isHarmonyOS = /harmonyos/.test(uaRaw) || /huawei|honor/.test(ua);
const isMobileToken = /mobile/.test(ua) || /sm-|mi |redmi|huawei|honor|oppo|vivo|oneplus/.test(ua);
const isHarmonyDesktop = isHarmonyOS && !isMobileToken && !isAndroid && !isiOS;
const isPWA = (typeof window !== 'undefined' && (
(window.matchMedia && window.matchMedia('(display-mode: standalone)').matches) ||
(nav.standalone === true)
)) || false;
const isMobile = (isiOS || isAndroid || (isHarmonyOS && !isHarmonyDesktop) || isMobileToken || isInAppWebView);
const info = { isMobile, isiOS, isAndroid, isHarmonyOS, isHarmonyDesktop, isWeChat, isQQ, isInAppWebView, isPWA, isiPad: isiPadUA || isIOSLikePad };
NeteaseMiniPlayer._uaCache = info;
return info;
}
/**
* 根据环境信息调整控件呈现(移动端隐藏音量等)
* @returns {void}
*/
applyResponsiveControls() {
const env = NeteaseMiniPlayer.getUAInfo();
const shouldHideVolume = !!env.isMobile;
this.element.classList.toggle('mobile-env', shouldHideVolume);
if (this.elements && this.elements.volumeContainer == null) {
this.elements.volumeContainer = this.element.querySelector('.volume-container');
}
if (this.elements.volumeContainer) {
if (shouldHideVolume) {
this.elements.volumeContainer.classList.add('sr-visually-hidden');
this.elements.volumeContainer.setAttribute('aria-hidden', 'false');
this.elements.volumeSlider?.setAttribute('aria-label', '音量控制(移动端隐藏,仅无障碍可见)');
} else {
this.elements.volumeContainer.classList.remove('sr-visually-hidden');
this.elements.volumeContainer.removeAttribute('aria-hidden');
this.elements.volumeSlider?.removeAttribute('aria-label');
}
}
}
/**
* 监听环境变化(方向、尺寸)并动态应用响应式策略
* @returns {void}
*/
setupEnvListeners() {
const reapply = () => this.applyResponsiveControls();
if (window.matchMedia) {
try {
const mq1 = window.matchMedia('(orientation: portrait)');
const mq2 = window.matchMedia('(orientation: landscape)');
mq1.addEventListener?.('change', reapply);
mq2.addEventListener?.('change', reapply);
} catch (e) {
mq1.onchange = reapply;
mq2.onchange = reapply;
}
} else {
window.addEventListener('orientationchange', reapply);
}
window.addEventListener('resize', reapply);
}
/**
* 绑定音频事件以同步UI、进度和歌词
* @returns {void}
*/
setupAudioEvents() {
this.audio.addEventListener('loadedmetadata', () => {
this.duration = this.audio.duration;
this.updateTimeDisplay();
});
this.audio.addEventListener('timeupdate', () => {
this.currentTime = this.audio.currentTime;
this.updateProgress();
this.updateLyrics();
this.updateTimeDisplay();
});
this.audio.addEventListener('ended', async () => {
await this.nextSong();
});
this.audio.addEventListener('error', async (e) => {
console.error('音频播放错误:', e);
console.error('错误详情:', {
code: e.target.error?.code,
message: e.target.error?.message,
src: e.target.src
});
this.showError('播放失败,尝试下一首');
setTimeout(async () => {
await this.nextSong();
}, 1000);
});
this.audio.addEventListener('abort', () => {
console.warn('音频加载被中断');
});
this.audio.addEventListener('stalled', () => {
console.warn('音频加载停滞');
});
this.audio.addEventListener('canplay', () => {
if (this.isPlaying && this.audio.paused) {
this.audio.play().catch(e => console.error('自动播放失败:', e));
}
});
this.audio.volume = this.volume;
this.updateVolumeDisplay();
}
/**
* 调用后端API(NetEase Cloud Music代理)
* @param {string} endpoint - 接口路径,如`/playlist/track/all`
* @param {Record<string, string|number|boolean>} [params] - 查询参数对象
* @returns {Promise<any>} 返回解析后的JSON数据
* @throws {Error} 当网络或接口返回错误码时抛出
*/
async apiRequest(endpoint, params = {}) {
const baseUrl = 'https://api.hypcvgm.top/NeteaseMiniPlayer/nmp.php';
const queryString = new URLSearchParams(params).toString();
const url = `${baseUrl}${endpoint}${queryString ? '?' + queryString : ''}`;
try {
const response = await fetch(url);
const data = await response.json();
if (data.code !== 200) {
throw new Error(`API错误: ${data.code}`);
}
return data;
} catch (error) {
console.error('API请求失败:', error);
throw error;
}
}
/**
* 生成缓存键,用于缓存系统
* @param {string} type - 资源类型,如`playlist_all`、`song`
* @param {string | number} id - 业务ID
* @returns {string} 拼接后的缓存键
* @description
* 简单的字符串拼接,格式为`{type}_{id}`
* 用于区分不同类型的缓存数据
* @example
* player.getCacheKey('playlist_all', '123456'); // 'playlist_all_123456'
* player.getCacheKey('song', 789); // 'song_789'
*/
getCacheKey(type, id) {
return `${type}_${id}`;
}
/**
* 设置缓存并附带过期时间
* @param {string} key - 缓存键
* @param {any} data - 缓存数据
* @param {number} [expiry=300000] - 过期毫秒数
* @returns {void}
*/
setCache(key, data, expiry = 5 * 60 * 1000) {
this.cache.set(key, {
data,
expiry: Date.now() + expiry
});
}
/**
* 读取缓存,过期则删除
* @param {string} key - 缓存键
* @returns {any | null} 命中返回数据,未命中或过期返回`null`
*/
getCache(key) {
const cached = this.cache.get(key);
if (cached && cached.expiry > Date.now()) {
return cached.data;
}
this.cache.delete(key);
return null;
}
/**
* 加载播放列表并更新展示
* @param {string | number} playlistId - 播放列表ID
* @returns {Promise<void>}
* @throws {Error} 当API请求失败或返回无效数据时抛出
* @example
* await player.loadPlaylist('123456789');
*/
async loadPlaylist(playlistId) {
const cacheKey = this.getCacheKey('playlist_all', playlistId);
let tracks = this.getCache(cacheKey);
if (!tracks) {
const response = await this.apiRequest('/playlist/track/all', {
id: playlistId,
limit: 1000,
offset: 0
});
tracks = response.songs;
this.setCache(cacheKey, tracks);
}
this.playlist = tracks.map(song => ({
id: song.id,
name: song.name,
artists: song.ar.map(ar => ar.name).join(', '),
album: song.al.name,
picUrl: song.al.picUrl,
duration: song.dt
}));
this.updatePlaylistDisplay();
}
/**
* 加载单曲信息并设置为播放源
* @param {string | number} songId - 歌曲ID
* @returns {Promise<void>}
* @throws {Error} 当API请求失败时抛出
* @example
* await player.loadSingleSong('123456789');
*/
async loadSingleSong(songId) {
const cacheKey = this.getCacheKey('song', songId);
let songData = this.getCache(cacheKey);
if (!songData) {
try {
const response = await this.apiRequest('/song/detail', { ids: songId });
if (response.songs && response.songs.length > 0) {
const song = response.songs[0];
songData = {
id: song.id,
name: song.name,
artists: song.ar.map(ar => ar.name).join(', '),
album: song.al.name,
picUrl: song.al.picUrl,
duration: song.dt
};
this.setCache(cacheKey, songData);
} else {
throw new Error('歌曲信息获取失败');
}
} catch (error) {
console.error('获取歌曲详情失败:', error);
songData = {
id: songId,
name: '歌曲加载失败',
artists: '未知艺术家',
album: '未知专辑',
picUrl: '',
duration: 0
};
}
}
this.playlist = [songData];
}
/**
* 根据当前索引加载歌曲、音频URL与歌词
* @returns {Promise<void>}
* @throws {Error} 当音频URL加载失败时抛出
* @description
* 1. 重置歌词状态
* 2. 更新歌曲信息与封面
* 3. 加载音频URL
* 4. 加载歌词(如果启用)
*/
async loadCurrentSong() {
if (this.playlist.length === 0) return;
if (this.showLyrics) {
this.elements.lyricLine.textContent = '♪ 加载歌词中... ♪';
this.elements.lyricTranslation.style.display = 'none';
this.elements.lyricLine.classList.remove('current', 'scrolling');
this.elements.lyricTranslation.classList.remove('current', 'scrolling');
this.lyrics = [];
this.currentLyricIndex = -1;
}
const song = this.playlist[this.currentIndex];
this.currentSong = song;
this.updateSongInfo(song);
if (song.picUrl) {
this.elements.albumCover.src = song.picUrl;
}
await this.loadSongUrl(song.id);
if (this.showLyrics) {
await this.loadLyrics(song.id);
}
}
/**
* 更新标题与艺术家信息
* @param {SongInfo} song - 歌曲信息
* @returns {void}
* @description
* 1. 更新歌曲标题
* 2. 截断艺术家名称(如果过长)
* 3. 设置title属性显示完整艺术家信息
*/
updateSongInfo(song) {
if (!song) return;