-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path赵佳琦(音乐播放器).txt
More file actions
2568 lines (2222 loc) · 83.3 KB
/
赵佳琦(音乐播放器).txt
File metadata and controls
2568 lines (2222 loc) · 83.3 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
main:
#define _CRT_SECURE_NO_WARNINGS
#define WINDOW_WIDTH 1280
#define WINDOW_HEIGHT 720
#include "player.h"
#pragma comment(lib, "winmm.lib")
int main() {
srand((unsigned int)time(NULL));
initgraph(WINDOW_WIDTH, WINDOW_HEIGHT);
/*if (!playIntroVideo()) {
return 0;
}
cleardevice();*/
IMAGE img;
loadimage(&img, "./source/picture/background.jpg", WINDOW_WIDTH, WINDOW_HEIGHT);
struct Playlist playlist;
initPlaylist(&playlist);
g_playlist = &playlist;
playlist.volume = 80;
loadDefaultPlaylist(&playlist);
if (playlist.musicSource == MUSIC_SOURCE_AUTO) {
playlist.visibleSongs = playlist.totalSongs;
}
// 创建左侧按钮
struct Button* lyricSourceBtn = creatButton(20, 515, 120, 40, RGB(150, 150, 255), "歌词:自动", 11);
struct Button* musicSourceBtn = creatButton(20, 560, 120, 40, RGB(150, 200, 255), "音乐:自动", 12);
struct Button* play = creatButton(20, 20, 120, 40, RGB(100, 200, 100), "播放", 0);
struct Button* pause = creatButton(20, 65, 120, 40, RGB(200, 200, 100), "暂停", 1);
struct Button* stop = creatButton(20, 110, 120, 40, RGB(200, 100, 100), "停止", 2);
struct Button* prev = creatButton(20, 155, 120, 40, RGB(100, 200, 200), "上一首", 3);
struct Button* next = creatButton(20, 200, 120, 40, RGB(100, 200, 200), "下一首", 4);
struct Button* mode = creatButton(20, 245, 120, 40, RGB(200, 100, 200), "播放模式", 5);
struct Button* addSongBtn = creatButton(20, 290, 120, 40, RGB(150, 150, 255), "添加歌曲", 6);
struct Button* showFavBtn = creatButton(20, 335, 120, 40, RGB(255, 200, 100), "显示收藏", 7);
struct Button* clearAllBtn = creatButton(20, 380, 120, 40, RGB(255, 100, 100), "清空歌单", 8);
struct Button* saveBtn = creatButton(20, 425, 120, 40, RGB(100, 255, 100), "保存歌单", 9);
struct Button* rescanBtn = creatButton(20, 470, 120, 40, RGB(150, 200, 255), "重新扫描", 10);
const char* musicSourceTexts[] = { "音乐:默认", "音乐:用户", "音乐:自动" };
SetTextButton(musicSourceBtn, musicSourceTexts[playlist.musicSource]);
// 创建右侧圆形控制按钮
int controlPanelX = WINDOW_WIDTH - 400;
int controlPanelY = WINDOW_HEIGHT - 120;
struct Button* prevCircle = createCircleButton(controlPanelX - 100, controlPanelY + 60, 30,
RGB(100, 200, 200), "<-", 20);
struct Button* playPauseCircle = createCircleButton(controlPanelX, controlPanelY + 60, 38,
RGB(100, 200, 100), "O", 21);
struct Button* nextCircle = createCircleButton(controlPanelX + 100, controlPanelY + 60, 30,
RGB(100, 200, 200), "->", 22);
BeginBatchDraw();
ExMessage msg;
int pauseFlag = 1;
int showOnlyFavorites = 0;
// 计算右侧歌词区域位置
int lyricX = WINDOW_WIDTH - 550;
int lyricY = 120;
int lyricWidth = 500;
int lyricHeight = 380;
// 计算进度条位置
int progressX = lyricX;
int progressY = lyricY + lyricHeight + 25;
int progressWidth = lyricWidth;
int progressHeight = 18;
// 计算音量控制位置
int volumeX = progressX;
int volumeY = progressY + 50;
int volumeWidth = 250;
int volumeHeight = 18;
// 状态变量
int currentMusicY = 200;
while (1) {
//[新增]放弃MCI,入侵太厉害了,转向可视化进度条
static int play_complete_count = 0; //设计帧数
if (g_playlist && g_playlist->current) {
char posStr[32] = { 0 };
char lenStr[32] = { 0 };
mciSendString("status mymusic position", posStr, sizeof(posStr), NULL); //获取播放歌曲位置
mciSendString("status mymusic length", lenStr, sizeof(lenStr), NULL); //获取歌曲总长度
//[核心]计算百分比
int current_ms = atoi(posStr); //将MCI传回的数据换算为毫秒
int total_ms = atoi(lenStr); //完整的毫秒数
if (total_ms > 0) {
int progress = (current_ms * 100) / total_ms;
if (progress >= 99) { //播放进度≥99%时启动切歌倒计时,倒计时采用帧计数方式,每帧计数+1,不同设备帧数不同可以自行调整停留帧数
play_complete_count++; //帧数++
if (play_complete_count >= 100) { //此处可以按需调整自己帧数
playNextSong(g_playlist);
play_complete_count = 0;
}
}
else {
play_complete_count = 0;
}
}
}
putimage(0, 0, &img);
// 绘制左侧歌单区域
drawCurrentSongInfo(&playlist, 180, 50);
drawPlayModeInfo(&playlist, 180, 100);
drawSongList(&playlist, showOnlyFavorites, 180, 200);
// 绘制右侧歌词区域
drawLyric(&playlist, lyricX, lyricY, lyricWidth, lyricHeight);
// 绘制进度条
char lengthStr[64] = "0";
mciSendString("status mymusic length", lengthStr, sizeof(lengthStr), NULL);
int totalLength_ms = atoi(lengthStr);
int currentPosition = getCurrentPlayPosition() * 1000;
if (totalLength_ms > 0) {
int progressPercent = (int)((float)currentPosition / totalLength_ms * 100);
drawProgressBar(progressX, progressY, progressWidth, progressHeight, progressPercent);
}
// 绘制音量控制
drawVolumeBar(&playlist, volumeX, volumeY, volumeWidth, volumeHeight);
// 更新歌词位置
updateLyricPosition(&playlist);
// 显示所有按钮
Show(play);
Show(pause);
Show(stop);
Show(prev);
Show(next);
Show(mode);
Show(addSongBtn);
Show(showFavBtn);
Show(clearAllBtn);
Show(saveBtn);
Show(rescanBtn);
Show(lyricSourceBtn);
Show(musicSourceBtn);
Show(prevCircle);
Show(playPauseCircle);
Show(nextCircle);
// 绘制状态信息
settextstyle(16, 0, "宋体");
settextcolor(LIGHTGRAY);
setbkmode(TRANSPARENT);
// 绘制歌词源信息
char lyricSourceMsg[50];
const char* lyricSourceNames[] = { "默认", "用户", "自动" };
sprintf(lyricSourceMsg, "歌词源: %s", lyricSourceNames[playlist.lyricSource]);
int lyricSourceWidth = textwidth(lyricSourceMsg);
setfillcolor(RGB(50, 50, 80));
setlinecolor(RGB(100, 100, 150));
setlinestyle(PS_SOLID, 2);
int lyricSourceY = 125;
solidroundrect(373, 95, 565, 122, 8, 8);
roundrect(373, 95, 565, 122, 8, 8);
settextcolor(LIGHTBLUE);
outtextxy(383, 99, lyricSourceMsg);
// 绘制音乐源信息
settextstyle(14, 0, "宋体");
settextcolor(LIGHTGRAY);
setbkmode(TRANSPARENT);
char musicSourceMsg[50];
const char* musicSourceNames[] = { "默认", "用户", "自动" };
sprintf(musicSourceMsg, "音乐源: %s", musicSourceNames[playlist.musicSource]);
int musicSourceWidth = textwidth(musicSourceMsg);
int musicSourceY = lyricSourceY + 30;
solidroundrect(373, 122, 565, 145, 8, 8);
roundrect(373, 122, 565, 145, 8, 8);
settextcolor(LIGHTCYAN);
outtextxy(383, 127, musicSourceMsg);
// 绘制当前歌曲信息
if (playlist.current) {
char currentMusicMsg[100];
enum MusicSource currentMusicSource = getMusicSourceForSong(&playlist, playlist.current);
const char* musicSourceText = "未知";
if (playlist.current->path) {
if (strstr(playlist.current->path, USER_MUSIC_FOLDER)) {
musicSourceText = "用户文件夹";
}
else if (strstr(playlist.current->path, DEFAULT_MUSIC_FOLDER)) {
musicSourceText = "默认文件夹";
}
}
sprintf(currentMusicMsg, "当前音乐: %s", musicSourceText);
int currentMusicWidth = textwidth(currentMusicMsg);
setfillcolor(RGB(40, 40, 60));
setlinecolor(RGB(80, 80, 120));
currentMusicY = musicSourceY + 30;
solidroundrect(373, 145, 565, 172, 8, 8);
roundrect(373, 145, 565, 172, 8, 8);
settextcolor(LIGHTBLUE);
outtextxy(383, 150, currentMusicMsg);
}
// 绘制状态信息
char statusMsg[100];
if (showOnlyFavorites) {
int favCount = countFavorites(&playlist);
sprintf(statusMsg, "当前: 显示收藏歌曲 (%d首)", favCount);
}
else {
sprintf(statusMsg, "当前: 显示全部歌曲 (%d首)", playlist.totalSongs);
}
setfillcolor(RGB(50, 50, 80));
setlinecolor(RGB(100, 100, 150));
setlinestyle(PS_SOLID, 2);
solidroundrect(165, 143, 373, 173, 8, 8);
roundrect(165, 143, 373, 173, 8, 8);
settextcolor(LIGHTGREEN);
outtextxy(180, 150, statusMsg);
// 绘制操作提示
settextstyle(16, 0, "宋体");
settextcolor(LIGHTGRAY);
setbkmode(TRANSPARENT);
setfillcolor(RGB(40, 40, 60));
setlinecolor(RGB(80, 80, 120));
setlinestyle(PS_SOLID, 2);
solidroundrect(15, 605, 140, 715, 10, 10);
roundrect(15, 605, 140, 715, 10, 10);
setfillcolor(RGB(60, 60, 90));
solidroundrect(20, 610, 140, 632, 5, 5);
roundrect(20, 610, 140, 632, 5, 5);
settextcolor(LIGHTGREEN);
outtextxy(20, 612, "操作提示:");
settextcolor(LIGHTGRAY);
outtextxy(20, 635, "左键: 播放歌曲");
outtextxy(20, 655, "右键: 删除歌曲");
outtextxy(20, 675, "中键: 切换收藏");
outtextxy(20, 695, "滚轮: 滚动歌单");
FlushBatchDraw();
if (peekmessage(&msg)) {
// 鼠标悬停检测
InButton(play, msg);
InButton(pause, msg);
InButton(stop, msg);
InButton(prev, msg);
InButton(next, msg);
InButton(mode, msg);
InButton(addSongBtn, msg);
InButton(showFavBtn, msg);
InButton(clearAllBtn, msg);
InButton(saveBtn, msg);
InButton(rescanBtn, msg);
InButton(lyricSourceBtn, msg);
InButton(musicSourceBtn, msg);
InButton(prevCircle, msg);
InButton(playPauseCircle, msg);
InButton(nextCircle, msg);
// 处理歌词区域滚轮
if (msg.message == WM_MOUSEWHEEL &&
msg.x >= lyricX && msg.x <= lyricX + lyricWidth &&
msg.y >= lyricY && msg.y <= lyricY + lyricHeight) {
if (playlist.current && playlist.current->lyric) {
if (msg.wheel < 0) {
scrollLyric(playlist.current->lyric, 1);
}
else {
scrollLyric(playlist.current->lyric, -1);
}
}
}
// 处理进度条点击
handleProgressBarClick(msg, progressX, progressY, progressWidth, progressHeight);
// 处理音量控制点击
handleVolumeBarClick(&playlist, msg, volumeX, volumeY, volumeWidth, volumeHeight);
// 处理歌单点击
int songClickResult = handleSongListClick(&playlist, showOnlyFavorites, msg, 180, 200);
if (songClickResult > 0) {
if (songClickResult == 1) {
pauseFlag = 0;
SetTextButton(pause, "暂停");
SetTextButton(playPauseCircle, "||");
}
continue;
}
// 处理右侧圆形按钮点击
if (msg.message == WM_LBUTTONDOWN) {
if (InButton(prevCircle, msg)) {
playPrevSong(&playlist);
pauseFlag = 0;
SetTextButton(pause, "暂停");
SetTextButton(playPauseCircle, "||");
}
else if (InButton(playPauseCircle, msg)) {
if (pauseFlag == 0) {
mciSendString("pause mymusic", 0, 0, 0);
pauseFlag = 1;
SetTextButton(playPauseCircle, "-");
SetTextButton(pause, "继续");
}
else {
mciSendString("resume mymusic", 0, 0, 0);
pauseFlag = 0;
SetTextButton(playPauseCircle, "||");
SetTextButton(pause, "暂停");
}
}
else if (InButton(nextCircle, msg)) {
playNextSong(&playlist);
pauseFlag = 0;
SetTextButton(pause, "暂停");
SetTextButton(playPauseCircle, "||");
}
}
// 歌词源按钮处理
if (msg.message == WM_LBUTTONDOWN && InButton(lyricSourceBtn, msg)) {
enum LyricSource newSource = (enum LyricSource)((playlist.lyricSource + 1) % 3);
setLyricSource(&playlist, newSource);
const char* sourceTexts[] = { "歌词:默认", "歌词:用户", "歌词:自动" };
SetTextButton(lyricSourceBtn, sourceTexts[newSource]);
if (playlist.current) {
reloadLyricsForCurrentSong(&playlist);
}
}
// 音乐源按钮处理
if (msg.message == WM_LBUTTONDOWN && InButton(musicSourceBtn, msg)) {
enum MusicSource newSource = (enum MusicSource)((playlist.musicSource + 1) % 3);
setMusicSource(&playlist, newSource);
const char* sourceTexts[] = { "音乐:默认", "音乐:用户", "音乐:自动" };
SetTextButton(musicSourceBtn, sourceTexts[newSource]);
cleardevice();
putimage(0, 0, &img);
if (playlist.current) {
playCurrentSong(&playlist);
if (pauseFlag == 0) {
mciSendString("pause mymusic", 0, 0, 0);
}
}
FlushBatchDraw();
continue; // 跳过本次循环的其他处理
}
// "显示收藏"按钮处理
if (msg.message == WM_LBUTTONDOWN && InButton(showFavBtn, msg)) {
showOnlyFavorites = !showOnlyFavorites;
playlist.scrollOffset = 0;
if (showOnlyFavorites) {
SetTextButton(showFavBtn, "显示全部");
if (playlist.current && !playlist.current->favorite) {
struct Song* firstFav = playlist.head;
while (firstFav && !firstFav->favorite) {
firstFav = firstFav->next;
}
if (firstFav) {
playlist.current = firstFav;
}
}
}
else {
SetTextButton(showFavBtn, "显示收藏");
}
}
// 其他按钮点击处理
if (msg.message == WM_LBUTTONDOWN) {
if (InButton(play, msg)) {
mciSendString("play mymusic", 0, 0, 0);
pauseFlag = 0;
SetTextButton(pause, "暂停");
SetTextButton(playPauseCircle, "||");
}
else if (InButton(pause, msg)) {
if (pauseFlag == 0) {
mciSendString("pause mymusic", 0, 0, 0);
pauseFlag = 1;
SetTextButton(pause, "继续");
SetTextButton(playPauseCircle, "-");
}
else {
mciSendString("resume mymusic", 0, 0, 0);
pauseFlag = 0;
SetTextButton(pause, "暂停");
SetTextButton(playPauseCircle, "||");
}
}
else if (InButton(stop, msg)) {
mciSendString("stop mymusic", 0, 0, 0);
pauseFlag = 0;
SetTextButton(pause, "暂停");
SetTextButton(playPauseCircle, "-");
}
else if (InButton(prev, msg)) {
playPrevSong(&playlist);
pauseFlag = 0;
SetTextButton(pause, "暂停");
SetTextButton(playPauseCircle, "||");
}
else if (InButton(next, msg)) {
playNextSong(&playlist);
pauseFlag = 0;
SetTextButton(pause, "暂停");
SetTextButton(playPauseCircle, "||");
}
else if (InButton(mode, msg)) {
changePlayMode(&playlist);
}
else if (InButton(addSongBtn, msg)) {
MessageBox(NULL,
"请将歌曲文件放入以下目录:\n"
"默认音乐: ./source/music/default/\n"
"用户音乐: ./source/music/user/\n\n"
"歌词文件放入以下目录:\n"
"默认歌词: ./source/lyrics/default/\n"
"用户歌词: ./source/lyrics/user/",
"文件存放提示", MB_OK | MB_ICONINFORMATION);
}
else if (InButton(clearAllBtn, msg)) {
int result = MessageBox(NULL, "确定要清空整个歌单吗?", "确认", MB_OKCANCEL | MB_ICONQUESTION);
if (result == IDOK) {
removeAllSongs(&playlist);
}
}
else if (InButton(saveBtn, msg)) {
saveUserPlaylist(&playlist);
settextstyle(16, 0, "宋体");
settextcolor(LIGHTGREEN);
outtextxy(400, 660, "歌单已保存到 user_playlist.txt");
FlushBatchDraw();
Sleep(1000);
}
else if (InButton(rescanBtn, msg)) {
char status[64] = "";
mciSendString("status mymusic mode", status, sizeof(status), 0);
int wasPlaying = (strcmp(status, "playing") == 0);
struct Song* currentBeforeScan = playlist.current;
settextstyle(16, 0, "宋体");
settextcolor(LIGHTGREEN);
const char* scanMsg = "正在扫描音乐文件夹...";
int msgWidth = textwidth(scanMsg);
setfillcolor(RGB(30, 60, 30));
setlinecolor(RGB(50, 100, 50));
setlinestyle(PS_SOLID, 2);
solidroundrect(395, 655, 395 + msgWidth + 10, 685, 5, 5);
roundrect(395, 655, 395 + msgWidth + 10, 685, 5, 5);
outtextxy(400, 660, scanMsg);
FlushBatchDraw();
scanMusicFolders(&playlist);
if (wasPlaying && playlist.current) {
playCurrentSong(&playlist);
}
else if (currentBeforeScan) {
playlist.current = currentBeforeScan;
}
FlushBatchDraw();
Sleep(1000);
}
}
// 键盘快捷键处理
if (msg.message == WM_KEYDOWN) {
switch (msg.vkcode) {
case VK_SPACE:
if (pauseFlag == 0) {
mciSendString("pause mymusic", 0, 0, 0);
pauseFlag = 1;
SetTextButton(pause, "继续");
SetTextButton(playPauseCircle, "-");
}
else {
mciSendString("resume mymusic", 0, 0, 0);
pauseFlag = 0;
SetTextButton(pause, "暂停");
SetTextButton(playPauseCircle, "||");
}
break;
case VK_LEFT:
playPrevSong(&playlist);
pauseFlag = 0;
SetTextButton(pause, "暂停");
SetTextButton(playPauseCircle, "||");
break;
case VK_RIGHT:
playNextSong(&playlist);
pauseFlag = 0;
SetTextButton(pause, "暂停");
SetTextButton(playPauseCircle, "||");
break;
case 'M':
case 'm':
changePlayMode(&playlist);
break;
case 'K':
case 'k':
{
enum LyricSource newSource = (enum LyricSource)((playlist.lyricSource + 1) % 3);
setLyricSource(&playlist, newSource);
const char* sourceTexts[] = { "歌词:默认", "歌词:用户", "歌词:自动" };
SetTextButton(lyricSourceBtn, sourceTexts[newSource]);
if (playlist.current) {
reloadLyricsForCurrentSong(&playlist);
}
}
break;
case 'J':
case 'j':
{
enum MusicSource newSource = (enum MusicSource)((playlist.musicSource + 1) % 3);
setMusicSource(&playlist, newSource);
const char* sourceTexts[] = { "音乐:默认", "音乐:用户", "音乐:自动" };
SetTextButton(musicSourceBtn, sourceTexts[newSource]);
if (playlist.current) {
playCurrentSong(&playlist);
if (pauseFlag == 0) {
mciSendString("pause mymusic", 0, 0, 0);
}
}
}
break;
case VK_UP:
if (playlist.volume < 100) {
playlist.volume += 5;
if (playlist.volume > 100) playlist.volume = 100;
char cmd[64];
sprintf(cmd, "setaudio mymusic volume to %d", playlist.volume * 10);
mciSendString(cmd, NULL, 0, NULL);
}
break;
case VK_DOWN:
if (playlist.volume > 0) {
playlist.volume -= 5;
if (playlist.volume < 0) playlist.volume = 0;
char cmd[64];
sprintf(cmd, "setaudio mymusic volume to %d", playlist.volume * 10);
mciSendString(cmd, NULL, 0, NULL);
}
break;
case VK_ESCAPE:
goto exit_loop;
}
}
// 鼠标滚轮处理
if (msg.message == WM_MOUSEWHEEL) {
if (msg.wheel < 0) {
if (showOnlyFavorites) {
scrollPlaylistFiltered(&playlist, showOnlyFavorites, 1);
}
else {
scrollPlaylist(&playlist, 1);
}
}
else {
if (showOnlyFavorites) {
scrollPlaylistFiltered(&playlist, showOnlyFavorites, -1);
}
else {
scrollPlaylist(&playlist, -1);
}
}
}
}
Sleep(10);
}
exit_loop:
EndBatchDraw();
stopCurrentSong();
saveUserPlaylist(&playlist);
// 释放所有资源
freePlaylist(&playlist);
DestroyButton(play);
DestroyButton(pause);
DestroyButton(stop);
DestroyButton(prev);
DestroyButton(next);
DestroyButton(mode);
DestroyButton(addSongBtn);
DestroyButton(showFavBtn);
DestroyButton(clearAllBtn);
DestroyButton(saveBtn);
DestroyButton(rescanBtn);
DestroyButton(lyricSourceBtn);
DestroyButton(musicSourceBtn);
DestroyButton(prevCircle);
DestroyButton(playPauseCircle);
DestroyButton(nextCircle);
closegraph();
printf("// 我想你可能想说拜拜!Ciallo~");
return 0;
}
头文件 :
#pragma once
#ifndef PLAYER_H
#define PLAYER_H
// === 配置文件路径 ===[新增]设计默认和用户双数据源
#define DEFAULT_PLAYLIST_FILE "default_playlist.txt"
#define USER_PLAYLIST_FILE "user_playlist.txt"
#define DEFAULT_LYRIC_FOLDER "./source/lyrics/default/"
#define USER_LYRIC_FOLDER "./source/lyrics/user/"
#define DEFAULT_MUSIC_FOLDER "./source/music/default/"
#define USER_MUSIC_FOLDER "./source/music/user/"
#define LYRIC_FILE_EXTENSION ".lrc"
// === 系统头文件 ===
#include <graphics.h>
#include <iostream>
#include <mmsystem.h>
#include <malloc.h>
#include <string.h>
#include <time.h>
#include <windows.h> //引入WindowsAPI
#include <stdlib.h>
#include <stdio.h>
#include <io.h> //文件操作
#include <direct.h>
#include <stdint.h>
#include <math.h>
#include <conio.h>
// === 枚举定义 ===
enum PlayMode {
PLAY_MODE_NORMAL = 0, // 顺序播放
PLAY_MODE_REPEAT_ONE, // 单曲循环
PLAY_MODE_REPEAT_ALL, // 列表循环
PLAY_MODE_SHUFFLE // 随机播放
};
//[新增]歌词源[自动实现2]
enum LyricSource {
LYRIC_SOURCE_DEFAULT = 0, // 从默认文件夹加载
LYRIC_SOURCE_USER = 1, // 从用户文件夹加载
LYRIC_SOURCE_AUTO = 2 // 自动选择(先用户后默认)
};
//[新增]音乐源[2]
enum MusicSource {
MUSIC_SOURCE_DEFAULT = 0, // 从默认文件夹加载
MUSIC_SOURCE_USER = 1, // 从用户文件夹加载
MUSIC_SOURCE_AUTO = 2 // 自动选择(先用户后默认)
};
// === 结构体定义 ===
//[新增]歌词行
struct LyricLine {
int time_ms; // 时间(毫秒)
char text[256]; // 歌词文本
struct LyricLine* next; // 下一行
};
//[新增]整首歌歌词
struct Lyric {
struct LyricLine* head; // 歌词行链表头
int total_lines; // 总行数
int current_line; // 当前行索引
int visible_count; // 可视行数
int scroll_offset; // 滚动偏移
};
//[新增]歌词数据+路径
struct Song {
char* name; // 歌曲名
char* path; // 文件路径
int duration; // 时长(秒)
int favorite; // 是否收藏(1=收藏,0=不收藏)
struct Song* next; // 下一首歌
struct Lyric* lyric; // 歌词数据
char* lyric_path; // 歌词文件路径
int visible;
};
//[新增]设计音乐源,歌词源
struct Playlist {
struct Song* head;
struct Song* current;
int totalSongs;
int visibleSongs; // 当前可见歌曲数量(根据音乐源过滤后)
int displayRows; // 歌单显示行数(固定值,如15)
int scrollOffset;
enum PlayMode playMode;
int volume;
enum LyricSource lyricSource;
enum MusicSource musicSource;
};
//[新增]圆形按钮设计
struct Button {
int x, y, w, h;
unsigned long curColor;
unsigned long oldColor;
char* str;
int id; // 按钮ID
int is_circle; // 判断圆形按钮
int radius; // 设计圆形半径
};
// === 函数声明 ===
// 按钮相关函数
struct Button* creatButton(int x, int y, int w, int h, unsigned long curColor, const char* str, int id);
struct Button* createCircleButton(int x, int y, int radius, unsigned long curColor, const char* str, int id);
void Show(struct Button* pButton);
int InButton(struct Button* pButton, ExMessage msg);
void SetTextButton(struct Button* pButton, const char* str);
void DestroyButton(struct Button* pButton);
// 播放列表相关函数
void initPlaylist(struct Playlist* playlist);
void addSong(struct Playlist* playlist, const char* name, const char* path);
void removeSong(struct Playlist* playlist, int index);
void removeAllSongs(struct Playlist* playlist);
void freePlaylist(struct Playlist* playlist);
struct Song* getSongAt(struct Playlist* playlist, int index);
int countFavorites(struct Playlist* playlist);
// 文件管理函数[新增设计两套源]
void savePlaylistToFile(struct Playlist* playlist, const char* filename); //[封装]保存歌列表到文件
void loadPlaylistFromFile(struct Playlist* playlist, const char* filename); //[封装]从文件加载歌单
void saveUserPlaylist(struct Playlist* playlist);
void loadUserPlaylist(struct Playlist* playlist);
void loadDefaultPlaylist(struct Playlist* playlist);
void loadDefaultPlaylistFromFile(struct Playlist* playlist);
void scanMusicFolder(struct Playlist* playlist, const char* folderPath);
void scanMusicFolders(struct Playlist* playlist);
char* buildMusicPath(const char* songName, enum MusicSource source);
// 音乐控制函数
void playCurrentSong(struct Playlist* playlist);
void playNextSong(struct Playlist* playlist);
void playPrevSong(struct Playlist* playlist);
void stopCurrentSong();
void changePlayMode(struct Playlist* playlist);
struct Song* getRandomSong(struct Playlist* playlist);
// 歌词相关函数
struct Lyric* loadLyricFromFile(const char* filename);
void freeLyric(struct Lyric* lyric);
void loadLyricForSong(struct Playlist* playlist, struct Song* song);
void reloadLyricsForCurrentSong(struct Playlist* playlist);
void drawLyric(struct Playlist* playlist, int x, int y, int width, int height);
void updateLyricPosition(struct Playlist* playlist);
void scrollLyric(struct Lyric* lyric, int direction);
void setLyricSource(struct Playlist* playlist, enum LyricSource source);
enum LyricSource getLyricSourceForSong(struct Playlist* playlist, struct Song* song);
// 音乐源相关函数
void setMusicSource(struct Playlist* playlist, enum MusicSource source);
enum MusicSource getMusicSourceForSong(struct Playlist* playlist, struct Song* song);
// 进度条相关函数
int getCurrentPlayPosition();
void drawProgressBar(int x, int y, int width, int height, int progress);
int handleProgressBarClick(ExMessage msg, int x, int y, int width, int height);
// 音量相关函数
void drawVolumeBar(struct Playlist* playlist, int x, int y, int width, int height);
int handleVolumeBarClick(struct Playlist* playlist, ExMessage msg, int x, int y, int width, int height);
// 界面绘制函数
void drawSongList(struct Playlist* playlist, int showOnlyFavorites, int startX, int startY);
void drawCurrentSongInfo(struct Playlist* playlist, int x, int y);
void drawPlayModeInfo(struct Playlist* playlist, int x, int y);
// 交互函数
int handleSongListClick(struct Playlist* playlist, int showOnlyFavorites, ExMessage msg, int startX, int startY);
void scrollPlaylist(struct Playlist* playlist, int direction);
void scrollPlaylistFiltered(struct Playlist* playlist, int showOnlyFavorites, int direction);
// 开场视频函数
int playIntroVideo();
extern struct Playlist* g_playlist; // 全局播放列表指针
#endif
函数 :
#define _CRT_SECURE_NO_WARNINGS
#include "player.h"
#pragma comment(lib, "winmm.lib")
// === 工具函数 ===
//[1]修改后的获取喜欢的歌单函数
int countVisibleFavorites(struct Playlist* playlist) {
if (!playlist || !playlist->head) return 0;
int count = 0;
struct Song* current = playlist->head;
while (current) {
if (current->visible && current->favorite) {
count++;
}
current = current->next;
}
return count;
}
//[新增]获取歌曲文件的相对路径[通过信息源类型选取文件夹][1]
char* buildMusicPath(const char* songName, enum MusicSource source) {
char* fullPath = (char*)malloc(512);
if (!fullPath) return NULL;
char nameWithoutExt[256];
strcpy(nameWithoutExt, songName);
char* dot = strrchr(nameWithoutExt, '.');
if (dot) *dot = '\0';
const char* folder = NULL;
if (source == MUSIC_SOURCE_DEFAULT) {
folder = DEFAULT_MUSIC_FOLDER;
}
else if (source == MUSIC_SOURCE_USER) {
folder = USER_MUSIC_FOLDER;
}
if (folder) {
sprintf(fullPath, "%s%s", folder, songName);
}
else {
strcpy(fullPath, songName);
}
return fullPath;
}
// === 随机播放函数 ===[1]
struct Song* getRandomSong(struct Playlist* playlist) {
if (!playlist || !playlist->head || playlist->totalSongs == 0) {
return NULL;
}
if (playlist->totalSongs == 1) {
return playlist->head;
}
int randomIndex;
struct Song* randomSong;
do {
randomIndex = rand() % playlist->totalSongs;
randomSong = getSongAt(playlist, randomIndex);
} while (randomSong == playlist->current && playlist->totalSongs > 1);
return randomSong;
}
// === 文件管理函数 ===
//保存到用户歌单[2]
void saveUserPlaylist(struct Playlist* playlist) {
savePlaylistToFile(playlist, USER_PLAYLIST_FILE);
}
//[2]
void loadUserPlaylist(struct Playlist* playlist) {
loadPlaylistFromFile(playlist, USER_PLAYLIST_FILE);
}
//[2]
void loadDefaultPlaylistFromFile(struct Playlist* playlist) {
loadPlaylistFromFile(playlist, DEFAULT_PLAYLIST_FILE);
}
// === 歌词相关函数 ===[1]
//[新增]从目标文件获取歌词[封装]
struct Lyric* loadLyricFromFile(const char* filename) {
FILE* file = fopen(filename, "r");
if (!file) {
return NULL;
}
struct Lyric* lyric = (struct Lyric*)malloc(sizeof(struct Lyric));
if (!lyric) {
fclose(file);
return NULL;
}
lyric->head = NULL;
lyric->total_lines = 0;
lyric->current_line = 0;
lyric->visible_count = 7;
lyric->scroll_offset = 0;
char line[512];
struct LyricLine* tail = NULL;
while (fgets(line, sizeof(line), file)) {
// 去除换行符和回车符
line[strcspn(line, "\r\n")] = 0;
// 跳过空行
if (strlen(line) == 0) continue;
// 解析时间戳
if (line[0] == '[') {
int minutes = 0, seconds = 0, milliseconds = 0;
int parsed = 0;
// 尝试解析 [mm:ss.xx] 格式
char* dot = strchr(line, '.');
if (dot) {
parsed = sscanf(line, "[%d:%d.%d]", &minutes, &seconds, &milliseconds);
}
else {
parsed = sscanf(line, "[%d:%d]", &minutes, &seconds);
}
if (parsed >= 2) {
struct LyricLine* newLine = (struct LyricLine*)malloc(sizeof(struct LyricLine));
if (!newLine) continue;
// 计算毫秒时间
newLine->time_ms = minutes * 60000 + seconds * 1000 + milliseconds;
// 提取歌词文本
char* text_start = strchr(line, ']');
if (text_start) {
text_start++;
while (*text_start == ' ') text_start++;
if (strlen(text_start) > 0) {
strcpy(newLine->text, text_start);
}
else {
strcpy(newLine->text, "|");
}
}
else {
strcpy(newLine->text, "|");
}
newLine->next = NULL;
// 按时间排序插入
if (!lyric->head) {
lyric->head = newLine;
tail = newLine;
}