forked from RhenCloud/NeteaseMiniPlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetease-mini-player-v2.js
More file actions
1790 lines (1692 loc) · 77.6 KB
/
Copy pathnetease-mini-player-v2.js
File metadata and controls
1790 lines (1692 loc) · 77.6 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])由zhecydn按照RhenCloud修改而来
*
* 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.
*/
(() => {
try {
const s = document.currentScript;
if (s && s.src) {
fetch(s.src, { mode: "cors", credentials: "omit" }).catch(() => {});
}
} catch (e) {}
})();
const GlobalAudioManager = {
currentPlayer: null,
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>`,
};
class NeteaseMiniPlayer {
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;
}
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;
const apiUrls = JSON.parse(element.dataset.apiUrls) || apiUrls === [];
// 读取 autoPause 配置
let autoPause = true; // 默认启用自动暂停
if (window.__NETEASE_MUSIC_CONFIG__?.autoPause !== undefined) {
autoPause = window.__NETEASE_MUSIC_CONFIG__.autoPause;
} else if (element.dataset.autoPause !== undefined) {
autoPause = element.dataset.autoPause !== "false";
}
return {
embed: isEmbed,
autoplay: element.dataset.autoplay === "true",
playlistId: element.dataset.playlistId,
songId: element.dataset.songId,
server: element.dataset.server,
position: finalPosition,
lyric: element.dataset.lyric !== "false",
theme: element.dataset.theme || "auto",
size: element.dataset.size || "compact",
defaultMinimized: defaultMinimized,
autoPauseDisabled: autoPauseDisabled,
autoPause: autoPause,
apiUrls: apiUrls,
};
}
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) {
this.play();
}
}
if (this.config.defaultMinimized && !this.config.embed && this.config.position !== "static") {
this.toggleMinimize();
}
} catch (error) {
console.error("播放器初始化失败:", error);
this.showError("加载失败,请稍后重试");
}
}
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");
}
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" && this.config.autoPause) {
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();
}
startIdleTimer() {
this.clearIdleTimer();
if (!this.shouldEnableIdleOpacity()) return;
this.idleTimeout = setTimeout(() => {
this.triggerFadeOut();
}, this.idleDelay);
}
clearIdleTimer() {
if (this.idleTimeout) {
clearTimeout(this.idleTimeout);
this.idleTimeout = null;
}
}
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);
}
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);
}
shouldEnableIdleOpacity() {
return this.isMinimized === true;
}
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"
);
}
}
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";
}
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;
}
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");
}
}
}
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);
}
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();
}
async apiRequest(endpoint, params = {}) {
const apiUrls = this.config.apiUrls;
for (const baseUrl of apiUrls) {
try {
const queryParams = {
server: this.config.server,
type: "playlist",
id: params.id,
...params,
};
const queryString = new URLSearchParams(queryParams).toString();
const url = `${baseUrl}?${queryString}`;
const response = await fetch(url, { mode: "cors", timeout: 5000 });
const data = await response.json();
if (!data) {
continue;
}
return {
code: 200,
songs: data || [],
};
} catch (error) {
console.warn(`API ${baseUrl} 请求失败:`, error);
continue;
}
}
throw new Error("所有 API 都请求失败");
}
getCacheKey(type, id) {
return `${type}_${id}`;
}
setCache(key, data, expiry = 5 * 60 * 1000) {
this.cache.set(key, {
data,
expiry: Date.now() + expiry,
});
}
getCache(key) {
const cached = this.cache.get(key);
if (cached && cached.expiry > Date.now()) {
return cached.data;
}
this.cache.delete(key);
return null;
}
async loadPlaylist(playlistId) {
const cacheKey = this.getCacheKey("playlist_all", playlistId);
let tracks = this.getCache(cacheKey);
if (!tracks) {
const response = await this.apiRequest("", {
id: playlistId,
});
tracks = response.songs || [];
this.setCache(cacheKey, tracks);
}
if (!tracks || tracks.length === 0) {
console.warn("歌单为空或无法加载,使用默认歌曲");
// 提供一个默认歌曲,避免播放器崩溃
// 使用特殊 ID "_empty" 表示这是一个占位符歌曲
this.playlist = [
{
id: "_empty",
name: "网络加载失败",
artists: "Cloud Home",
album: "Demo",
picUrl: "",
duration: 0,
},
];
return;
}
this.playlist = tracks.map((song) => {
// Meting API 返回格式可能变化,需要从多个地方获取 ID
let songId = song.id || song.mid;
// 如果没有直接的 ID,尝试从 URL 中提取
if (!songId && song.url) {
const urlMatch = song.url.match(/id=(\d+)/);
songId = urlMatch ? urlMatch[1] : null;
}
// 或从 lrc URL 中提取
if (!songId && song.lrc) {
const lrcMatch = song.lrc.match(/id=(\d+)/);
songId = lrcMatch ? lrcMatch[1] : null;
}
if (!songId) {
console.warn("歌曲缺少ID,无法播放:", song.title || song.name);
}
return {
id: songId || "",
name: song.name || song.title || "Unknown",
artists: song.artist || song.author || "Unknown Artist",
album: song.album || "Unknown Album",
picUrl: song.pic || song.cover || "",
duration: song.duration
? typeof song.duration === "string"
? parseInt(song.duration) * 1000
: song.duration * 1000
: 0,
// 保存原始 API 返回的 URL 供后续使用
rawUrl: song.url || null,
rawLyricUrl: song.lrc || null,
};
});
this.setCache(cacheKey, tracks);
this.updatePlaylistDisplay();
}
async loadSingleSong(songId) {
const cacheKey = this.getCacheKey("song", songId);
let songData = this.getCache(cacheKey);
if (!songData) {
const apiUrls = [
`https://api.qijieya.cn/meting/?type=song&id=${songId}`,
`https://musicapi.chuyel.top:443/meting/api?server=tencent&type=song&id=${songId}`,
];
for (const url of apiUrls) {
try {
const response = await fetch(url);
const songs = await response.json();
if (songs && songs.length > 0) {
const song = songs[0];
songData = {
id: song.id || song.mid || songId,
name: song.name || song.title || "Unknown",
artists: song.artist || song.author || "Unknown Artist",
album: song.album || "Unknown Album",
picUrl: song.pic || song.cover || "",
duration: song.duration
? typeof song.duration === "string"
? parseInt(song.duration) * 1000
: song.duration * 1000
: 0,
};
this.setCache(cacheKey, songData);
break;
}
} catch (error) {
console.warn("从此API获取歌曲失败:", error);
continue;
}
}
if (!songData) {
throw new Error("歌曲信息获取失败");
}
}
this.playlist = [songData];
}
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);
if (this.showLyrics) {
await this.loadLyrics(song);
}
}
updateSongInfo(song) {
if (!song) return;
this.elements.songTitle.textContent = song.name || "未知歌曲";
if (song.artists) {
const truncatedArtist = this.truncateArtistName(song.artists);
this.elements.songArtist.textContent = truncatedArtist;
if (truncatedArtist !== song.artists) {
this.elements.songArtist.setAttribute("title", song.artists);
} else {
this.elements.songArtist.removeAttribute("title");
}
}
}
truncateArtistName(artistText) {
if (!artistText) return "";
const tempElement = document.createElement("span");
tempElement.style.visibility = "hidden";
tempElement.style.position = "absolute";
tempElement.style.fontSize = "12px";
tempElement.style.fontFamily = '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif';
tempElement.textContent = artistText;
document.body.appendChild(tempElement);
const fullWidth = tempElement.offsetWidth;
const availableWidth = 200;
if (fullWidth <= availableWidth) {
document.body.removeChild(tempElement);
return artistText;
}
const artists = artistText.split(" / ");
let result = "";
let currentWidth = 0;
for (let i = 0; i < artists.length; i++) {
const testText = result ? `${result} / ${artists[i]}` : artists[i];
tempElement.textContent = testText + "...";
const testWidth = tempElement.offsetWidth;
if (testWidth > availableWidth) {
if (result) {
break;
} else {
const artist = artists[i];
for (let j = 1; j < artist.length; j++) {
const partialArtist = artist.substring(0, j);
tempElement.textContent = partialArtist + "...";
if (tempElement.offsetWidth > availableWidth) {
result = artist.substring(0, Math.max(1, j - 1));
break;
}
result = partialArtist;
}
break;
}
}
result = testText;
}
document.body.removeChild(tempElement);
return result + (result !== artistText ? "..." : "");
}
async loadSongUrl(song) {
if (!song || !song.id || song.id === "_empty") {
console.warn("歌曲对象无效,跳过加载音频URL");
return;
}
const songId = String(song.id); // 确保转换为字符串
const cacheKey = this.getCacheKey("song_url", songId);
let urlData = this.getCache(cacheKey);
if (!urlData) {
// 优先尝试使用 playlist 中已有的 URL
if (song.rawUrl) {
try {
const response = await fetch(song.rawUrl, { method: "HEAD" });
if (response.ok) {
urlData = { url: song.rawUrl };
this.setCache(cacheKey, urlData, 30 * 60 * 1000);
}
} catch (error) {
console.warn("验证歌单URL失败:", error);
}
}
// 如果没有原始 URL,尝试从 API 获取
if (!urlData) {
const baseUrls = this.config.apiUrls;
const apiUrls = baseUrls.map((baseUrl) => `${baseUrl}?server=netease&type=song&id=${songId}`);
for (const url of apiUrls) {
try {
const response = await fetch(url, { mode: "cors" });
const data = await response.json();
if (data && data.length > 0) {
urlData = {
url: data[0].url || data[0],
};
this.setCache(cacheKey, urlData, 30 * 60 * 1000);
break;
}
} catch (error) {
if (error.name === 'AbortError') {
// 这是正常的中断,不需要 console.warn
return;
}
console.warn("从此API获取失败:", error);
continue;
}
}
}
}
if (urlData && urlData.url) {
const httpsUrl = this.ensureHttps(urlData.url);
this.audio.src = httpsUrl;
} else {
console.warn("无法获取音频URL");
}
}
ensureHttps(url) {
if (!url) return url;
if (url.includes("music.126.net")) {
return url.replace(/^http:\/\//, "https://");
}
if (url.startsWith("http://")) {
return url.replace("http://", "https://");
}
return url;
}
async loadLyrics(song) {
if (!song || !song.id || song.id === "_empty") {
console.warn("歌曲对象无效,跳过加载歌词");
return;
}
const songId = String(song.id); // 确保转换为字符串
const cacheKey = this.getCacheKey("lyric", songId);
let lyricData = this.getCache(cacheKey);
if (!lyricData) {
const baseUrls = this.config.apiUrls;
const apiUrls = baseUrls.map((baseUrl) => `${baseUrl}?server=netease&type=lrc&id=${songId}`);
for (const url of apiUrls) {
try {
const response = await fetch(url);
const contentType = response.headers.get("content-type") || "";
if (contentType.includes("application/json")) {
lyricData = await response.json();
} else {
// API 直接返回 lrc 文件内容(纯文本)
const lrcText = await response.text();
lyricData = { lrc: { lyric: lrcText } };
}
if (lyricData) {
this.setCache(cacheKey, lyricData, 60 * 60 * 1000);
break;
}
} catch (error) {
console.warn("从此API获取歌词失败:", error);
continue;
}
}
if (!lyricData) {
console.warn("无法获取歌词");
this.lyrics = [];
return;
}
}
this.parseLyrics(lyricData);
}
parseLyrics(lyricData) {
this.lyrics = [];
this.currentLyricIndex = -1;
if (!lyricData || (!lyricData.lrc?.lyric && !lyricData.tlyric?.lyric)) {
this.elements.lyricLine.textContent = "暂无歌词";
this.elements.lyricTranslation.style.display = "none";
this.elements.lyricLine.classList.remove("current", "scrolling");
this.elements.lyricTranslation.classList.remove("current", "scrolling");
return;
}
// 处理 lrc 数据可能是字符串或对象的情况
const lrcContent = typeof lyricData.lrc === "string" ? lyricData.lrc : lyricData.lrc?.lyric || "";
const tlyricContent = typeof lyricData.tlyric === "string" ? lyricData.tlyric : lyricData.tlyric?.lyric || "";
const lrcLines = lrcContent.split("\n");
const tlyricLines = tlyricContent ? tlyricContent.split("\n") : [];
const lrcMap = new Map();
lrcLines.forEach((line) => {
const match = line.match(/\[(\d{2}):(\d{2})\.(\d{2,3})\](.*)/);
if (match) {
const minutes = parseInt(match[1]);
const seconds = parseInt(match[2]);
const milliseconds = parseInt(match[3].padEnd(3, "0"));
const time = minutes * 60 + seconds + milliseconds / 1000;
const text = match[4].trim();
if (text) {
lrcMap.set(time, text);
}
}
});
const tlyricMap = new Map();
tlyricLines.forEach((line) => {
const match = line.match(/\[(\d{2}):(\d{2})\.(\d{2,3})\](.*)/);
if (match) {
const minutes = parseInt(match[1]);
const seconds = parseInt(match[2]);
const milliseconds = parseInt(match[3].padEnd(3, "0"));
const time = minutes * 60 + seconds + milliseconds / 1000;
const text = match[4].trim();
if (text) {
tlyricMap.set(time, text);
}
}
});
const allTimes = Array.from(new Set([...lrcMap.keys(), ...tlyricMap.keys()])).sort((a, b) => a - b);
this.lyrics = allTimes.map((time) => ({
time,
text: lrcMap.get(time) || "",
translation: tlyricMap.get(time) || "",
}));
this.currentLyricIndex = -1;
this.updateLyrics();
}
async togglePlay() {
if (this.isPlaying) {
this.pause();
} else {
await this.play();
}
}
async play() {
GlobalAudioManager.setCurrent(this);
try {
await this.audio.play();
this.isPlaying = true;
this.elements.playIcon.style.display = "none";
this.elements.pauseIcon.style.display = "inline";
this.elements.albumCover.classList.add("playing");
this.element.classList.add("player-playing");
} catch (error) {
console.error("播放失败:", error);
this.showError("播放失败");
}
}
pause() {
this.audio.pause();
this.isPlaying = false;
this.elements.playIcon.style.display = "inline";
this.elements.pauseIcon.style.display = "none";
this.elements.albumCover.classList.remove("playing");