-
Notifications
You must be signed in to change notification settings - Fork 1
/
MusicPlayer 8.0 FINAL.h.txt
1441 lines (1154 loc) · 41.1 KB
/
MusicPlayer 8.0 FINAL.h.txt
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
#include <stdio.h>
#include <io.h>
#include <Windows.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <conio.h>
#include <mmsystem.h>
#include <time.h>
#include <stdarg.h>
#define LENGTH 500
#pragma comment(lib,"winmm.lib")
HANDLE hOutput, hOutBuf, hSet; //控制台屏幕缓冲区句柄
COORD coord;
DWORD bytes = 0;
char PrintfBuff[LENGTH];
HANDLE hOut[2]; //两个缓冲区的句柄
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
//获取系统默认缓冲区句柄
enum { FATAL, ERROR_, WARNING, INFO, DEBUG }; //log日志记录类型 一共分FATAL ERROR_ WARNING INFO DEBUG 五种
//播放列表对应链表的定义
typedef struct MusicNode {
int ReId; //相对ID,满足1~n严格递增
char name[100]; //歌曲名称
struct MusicNode* next; //next指针
}MusicNode, * LinkNode;
struct lrc {
int time; //每句歌词对应歌词时间
char lyric[100]; //歌词内容
}LRC[1000]; //LRC[i].lyric表示第IDInput首歌的第i行歌词
//同一时间只读取一首歌的歌词
int LinesNumber; //LinesNumber表示第IDInput首歌的总行数
LinkNode head[100] = { NULL }; //100个歌单初始化为空链表
const char* Mode[4] = { "0", "顺序播放","单曲循环","随机播放" };
char _PATH[100] = { NULL }; //用户自定义mp3文件以及歌单txt文件储存路径
char alls[1000][100]; //库中所有歌曲名称用alls储存,带后缀
char listname[100][100]; //自定义播放列表名称,输入时不带后缀,储存时带后缀
//新建歌单需要输入listname,通过addone添加歌曲
int ListSize[100]; //ListSize[i]表示第i个歌单有多少首歌
int ListNumber = 0; //歌单的数量
int MusicNumber = 0; //库中歌曲个数
int Mci_Flag = 0; //判断是否调用MusicOpen 0表示未调用函数
int Bro_Flag = -1; //播放/暂停 0表示暂停 1表示播放 -1表示未打开音乐
int ReId = -1; //在当前歌单中的相对id
int ListFlag = -1; //当前播放歌曲所在歌单的编号
int BackFlag = -1; //用于解决返回主菜单时音乐无法播放的问题
int ModeFlag = 1; //播放模式 1顺序播放(默认) 2单曲循环 3随机播放
int p = 1; //用于确定当前歌词行数,关闭音乐时置1代表下一次从歌词第一行开始显示
int LyricFlag = 1; //遍历p,将值赋给LyricFlag,记录当前是第几行歌词
char BroName[100]; //当前播放的歌曲名称
int IDInput = -1; //要播放的歌曲在当前列表的序号
int LyricShowFlag; //0表示当前打开的这首歌没有lrc,1表示有
bool BuffSwitch; //缓冲区转换标志
//日志记录函数
void log_record(int error_level, const char* format, ...)
{
va_list args;
FILE* fp = NULL;
char time_str[32];
char file_name[256];
va_start(args, format);
time_t time_log = time(NULL);
strftime(file_name, sizeof(file_name), "%Y-%m-%d_log_history.log", localtime(&time_log));
if ((fp = fopen(file_name, "a+")) != NULL)
{
strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", localtime(&time_log));
if (error_level == (int)FATAL)
{
fprintf(fp, "[%s]-[%s]-[%s] :> ", time_str, "FATAL", __FILE__);
vfprintf(fp, format, args);
fprintf(fp, "\n");
}
if (error_level == (int)ERROR_)
{
fprintf(fp, "[%s]-[%s]-[%s] :> ", time_str, "ERROR", __FILE__);
vfprintf(fp, format, args);
fprintf(fp, "\n");
}
else if (error_level == (int)WARNING)
{
fprintf(fp, "[%s]-[%s]-[%s] :> ", time_str, "WARNINGs", __FILE__);
vfprintf(fp, format, args);
fprintf(fp, "\n");
}
else if (error_level == (int)INFO)
{
fprintf(fp, "[%s]-[%s]-[%s] :> ", time_str, "INFO", __FILE__);
vfprintf(fp, format, args);
fprintf(fp, "\n");
}
else if (error_level == (int)DEBUG)
{
fprintf(fp, "[%s]-[%s]-[%s] :> ", time_str, "DEBUG", __FILE__);
vfprintf(fp, format, args);
fprintf(fp, "\n");
}
fclose(fp);
}
va_end(args);
}
void initialalls(); //扫描本地mp3文件,将歌名信息储存到alls数组中
//将字符数组path存入alls,同时更新MusicNumber
inline void addalls(const char* path)
{
strcpy(alls[++MusicNumber], path);
}
void addone(const char* name, int ListFlag); //将名称为name的歌曲,添加到head[LishFlag]尾部
void PrintList(int ListFlag); //将head[ListFlag]中的歌曲打印
void SaveList(int ListFlag); //启动程序时初始化歌单 程序结束时再保存歌单,遍历链表,覆盖原txt文档
char* GetName(int ReId, int ListFlag); //返回head[ListFlag]的第ReId首歌的名称,保证ReId不越界
void initiallist(); //打开程序时读取txt,创建list
void addlist(int n, const char* txtname); //从txtname名称的文件读取信息存到head[n]中
void deleteone(int n, int ListFlag); //删除head[ListFlag]中第n首歌
char* GetPrev(); //保证ReId不越界,返回上一首歌的名称,每次返回时用strcpy传给另一个字符数组
char* GetNext(); //保证ReId不越界,返回下一首歌的名称,不改变ReId,IDInput等的值
bool exist(const char* path); //判断名称相对路径为path的文件是否存在
void Readlrc(); //根据当前的IDInput和ListFlag读取歌词信息,存入LRC结构体
int DeleteList(int n); //删除编号为n的歌单,删除成功返回0,否则返回-1
void Play();
//根据歌曲当前播放进度,用字符数组储存歌曲播放进度条的显示样例,返回该数组的首地址
char* Getprogress(int music_alltime, int music_timeing_ms);
char* Getvolumeline(int vol); //用字符数组储存当前音量的显示样例,返回该数组的首地址
/*********************************************************
实现一些与mci相关的函数
*********************************************************/
//表示链接xxx.lib这个库,告诉编译器你要用到xxx.lib库
//和在工程设置里写上链入xxx.lib的效果一样,
//不过这种方法写的 程序别人在使用你的代码的时候就不用再设置工程settings了。
#define CTRLCMD_SIZE 256
//打开设备
void MusicOpen(const char* pPath)
{
char CtrlCmd[CTRLCMD_SIZE];
char pathindex[100] = { NULL };
strcpy(pathindex, _PATH);
strcat(pathindex, pPath);
sprintf(CtrlCmd, "open \"%s\" alias mymusic", pathindex);//alias别名
mciSendString(CtrlCmd, NULL, 0, NULL); //向一个MCI的设备驱动程序发送一个字符串
Mci_Flag = 1;
}
//播放音乐
void MusicPlay()
{
mciSendString("setaudio mymusic volume to 500", NULL, 0, NULL);
mciSendString("play mymusic", NULL, 0, NULL);
}
//停止音乐
void MusicStop()
{
mciSendString("stop mymusic", NULL, 0, NULL);
}
//关闭音乐
void MusicClose()
{
char c[100];
int i = mciSendString("close mymusic", NULL, 0, NULL);//函数成功调用,则返回0,调用失败返回错误代码
if (i) //调用函数失败
{
mciGetErrorString(i, c, 100); //用于返回一个错误代码的文本描述,C是用来接收返回的文本描述的缓冲区指针
printf("%s\n", c);
}
Mci_Flag = 0;
}
//暂停音乐
void MusicPause()
{
char c[100];
int i = mciSendStringA("pause mymusic", NULL, 0, NULL);
if (i)
{
mciGetErrorString(i, c, 100);
printf("%s\n", c);
}
}
//返回当前音量大小
int MusicGetVolume(void)
{
char c[100];
int v0 = 0;
mciSendString("status mymusic volume", c, sizeof(c), NULL);
v0 = atoi(c); //c一个远指针,它指向由应用程序返回的字符串缓冲区,sizeof(c)表示缓冲区的大小
return v0; //atoi把字符串转成整型,参数是const char* 但是音量在哪里?为什么一定字符串里面是数字字符?
}
//增加音量
void MusicSetVolumeUp(void)
{
int volume = 0;
char cmd[100];
volume = MusicGetVolume();
if (volume + 100 <= 1000)
{
wsprintf(cmd, "setaudio mymusic volume to %d", volume + 100);//将一系列的字符和数值输入到缓冲区
mciSendString(cmd, NULL, 0, NULL);
}
else
{
mciSendString("setaudio mymusic volume to 1000", NULL, 0, NULL);
}
}
//减小音量
void MusicSetVolumeDown(void)
{
int volume = 0;
char cmd[100];
volume = MusicGetVolume();
if (volume - 100 >= 0)
{
wsprintf(cmd, "setaudio mymusic volume to %d", volume - 100);//将一系列的字符和数值输入到缓冲区
mciSendString(cmd, NULL, 0, NULL);
}
else
{
mciSendString("setaudio mymusic volume to 0", NULL, 0, NULL);
}
}
//快进5秒
void MusicFastForward(void)
{
char c[100];
char cmd[100];
char c1[100];
int iCurrent, i;
mciSendString("pause mymusic", NULL, 0, NULL);
mciSendString("status mymusic position", c, sizeof(c), NULL);
iCurrent = atoi(c); //当前播放位置
wsprintf(cmd, "seek mymusic to %d", iCurrent + 5000);
i = mciSendString(cmd, NULL, 0, NULL);
if (i)
{
mciGetErrorString(i, c1, 100);
printf("%s\n", c1);
}
else printf("%d\n", mciSendString("play mymusic", NULL, 0, NULL));
}
//快退5秒 默认快进快退合法
void MusicFastBackward(void)
{
char c[100];
char cmd[100];
char c1[100];
int iCurrent, i;
mciSendString("pause mymusic", NULL, 0, NULL);
mciSendString("status mymusic position", c, sizeof(c), NULL);//用atoi表示当前位置是提取音乐序号?
iCurrent = atoi(c); //当前播放位置
wsprintf(cmd, "seek mymusic to %d", iCurrent - 5000); //当前播放位置-5000
i = mciSendString(cmd, NULL, 0, NULL);
if (i)
{
mciGetErrorString(i, c1, 100);
printf("%s\n", c1);
}
else
{
mciSendString("play mymusic", NULL, 0, NULL);
}
}
//播放音乐,从绝对路径播放
void Play()
{
if (1 == Mci_Flag) //1表示调用过MusicOpen函数
{
MusicClose();
Bro_Flag = -1; //-1表示未打开音乐
log_record(ERROR_, "LINE: %d music open failed", __LINE__ - 2);
}
MusicOpen(BroName);
Readlrc();
MusicPlay();
Bro_Flag = 1; //1表示当前音乐状态是播放
system("cls");
}
//音乐的播放/暂停
void MusicPauseBroadcast(void)
{
if (Bro_Flag == 1) //1表示当前音乐状态是播放
{
MusicPause();
log_record(INFO, "LINE: %d music pause", __LINE__ - 1);
Bro_Flag = 0;
}
else if (Bro_Flag == 0) //0表示当前音乐状态是暂停
{
MusicPlay();
log_record(INFO, "LINE: %d music play", __LINE__ - 1);
Bro_Flag = 1;
}
else
{
printf("调用函数失败,可能未打开音乐文件\n");
log_record(ERROR_, "LINE: %d func call failed", __LINE__ - 1);
return;
}
}
//双缓冲区初始化
void DoubleBuffInitial()
{
//创建新的控制台缓冲区
hOutBuf = CreateConsoleScreenBuffer(
GENERIC_WRITE, //定义进程可以往缓冲区写数据
FILE_SHARE_WRITE, //定义缓冲区可共享写权限
NULL,
CONSOLE_TEXTMODE_BUFFER,
NULL
);
hOutput = CreateConsoleScreenBuffer(
GENERIC_WRITE, //定义进程可以往缓冲区写数据
FILE_SHARE_WRITE, //定义缓冲区可共享写权限
NULL,
CONSOLE_TEXTMODE_BUFFER,
NULL
);
hOut[0] = hOutBuf;
hOut[1] = hOutput;
//隐藏两个缓冲区的光标
CONSOLE_CURSOR_INFO cursor;
cursor.bVisible = FALSE;
cursor.dwSize = sizeof(cursor);
SetConsoleCursorInfo(hOutput, &cursor);
SetConsoleCursorInfo(hOutBuf, &cursor);
}
//双缓冲显示播放界面
void DoubleBuffSet(int music_timeing_ms, int music_alltime, int music_timeing, char* PreSong, char* NextSong)
{
if (LyricShowFlag)
{
memset(PrintfBuff, 0, sizeof PrintfBuff);
for (int i = 0; i < strlen(BroName) - 4; i++)
{
PrintfBuff[i] = BroName[i];
}
coord.X = 60;
coord.Y = 1;
WriteConsoleOutputCharacterA(hSet, PrintfBuff, LENGTH, coord, &bytes);
for (p = 1; music_timeing_ms >= LRC[p].time; p++);
LyricFlag = --p;
memset(PrintfBuff, 0, sizeof PrintfBuff);
if (LyricFlag >= 5)
{
strcpy(PrintfBuff, LRC[LyricFlag - 4].lyric);
}
coord.Y += 2;
WriteConsoleOutputCharacterA(hSet, PrintfBuff, LENGTH, coord, &bytes);
memset(PrintfBuff, 0, sizeof PrintfBuff);
if (LyricFlag >= 4)
{
strcpy(PrintfBuff, LRC[LyricFlag - 3].lyric);
}
coord.Y += 2;
WriteConsoleOutputCharacterA(hSet, PrintfBuff, LENGTH, coord, &bytes);
memset(PrintfBuff, 0, sizeof PrintfBuff);
if (LyricFlag >= 3)
{
strcpy(PrintfBuff, LRC[LyricFlag - 2].lyric);
}
coord.Y += 2;
WriteConsoleOutputCharacterA(hSet, PrintfBuff, LENGTH, coord, &bytes);
memset(PrintfBuff, 0, sizeof PrintfBuff);
if (LyricFlag >= 2)
{
strcpy(PrintfBuff, LRC[LyricFlag - 1].lyric);
}
coord.Y += 2;
WriteConsoleOutputCharacterA(hSet, PrintfBuff, LENGTH, coord, &bytes);
memset(PrintfBuff, 0, sizeof PrintfBuff);
strcpy(PrintfBuff, LRC[LyricFlag].lyric);
coord.Y += 2;
WriteConsoleOutputCharacterA(hSet, PrintfBuff, LENGTH, coord, &bytes);
strcpy(PrintfBuff, "★");
coord.X = 58;
WriteConsoleOutputCharacterA(hSet, PrintfBuff, 2, coord, &bytes);
coord.X = 60;
memset(PrintfBuff, 0, sizeof PrintfBuff);
if (LyricFlag + 1 <= LinesNumber)
{
strcpy(PrintfBuff, LRC[LyricFlag + 1].lyric);
}
coord.Y += 2;
WriteConsoleOutputCharacterA(hSet, PrintfBuff, LENGTH, coord, &bytes);
memset(PrintfBuff, 0, sizeof PrintfBuff);
if (LyricFlag + 2 <= LinesNumber)
{
strcpy(PrintfBuff, LRC[LyricFlag + 2].lyric);
}
coord.Y += 2;
WriteConsoleOutputCharacterA(hSet, PrintfBuff, LENGTH, coord, &bytes);
memset(PrintfBuff, 0, sizeof PrintfBuff);
if (LyricFlag + 3 <= LinesNumber)
{
strcpy(PrintfBuff, LRC[LyricFlag + 3].lyric);
}
coord.Y += 2;
WriteConsoleOutputCharacterA(hSet, PrintfBuff, LENGTH, coord, &bytes);
memset(PrintfBuff, 0, sizeof PrintfBuff);
if (LyricFlag + 4 <= LinesNumber)
{
strcpy(PrintfBuff, LRC[LyricFlag + 4].lyric);
}
coord.Y += 2;
WriteConsoleOutputCharacterA(hSet, PrintfBuff, LENGTH, coord, &bytes);
}
else {
for (coord.Y = 1; coord.Y < 22; coord.Y += 2)
{
memset(PrintfBuff, 0, sizeof PrintfBuff);
WriteConsoleOutputCharacterA(hSet, PrintfBuff, LENGTH, coord, &bytes);
}
strcpy(PrintfBuff, "★");
coord.X = 58;
coord.Y = 10;
WriteConsoleOutputCharacterA(hSet, PrintfBuff, 2, coord, &bytes);
memset(PrintfBuff, 0, sizeof PrintfBuff);
strcpy(PrintfBuff, "未找到歌词文件");
coord.X = 60;
WriteConsoleOutputCharacterA(hSet, PrintfBuff, LENGTH, coord, &bytes);
}
char progressline[101] = { NULL };
strcpy(progressline, Getprogress(music_alltime, music_timeing_ms));
coord.X = 18;
coord.Y = 23;
WriteConsoleOutputCharacterA(hSet, progressline, 100, coord, &bytes);
memset(PrintfBuff, 0, sizeof PrintfBuff);
sprintf(PrintfBuff, "Time:%d:%02d-%d:%02d", music_timeing / 60, music_timeing % 60, music_alltime / 60, music_alltime % 60); // 显示当前时间和总时间,歌曲名,当前音量和模式
coord.X = 0;
coord.Y = 23;
WriteConsoleOutputCharacterA(hSet, PrintfBuff, 15, coord, &bytes);
memset(PrintfBuff, 0, sizeof PrintfBuff);
strcat(PrintfBuff, "当前音量: ");
char volumeline[13] = { NULL };
strcpy(volumeline, Getvolumeline(MusicGetVolume()));
strcat(PrintfBuff, volumeline);
coord.X = 0;
coord.Y = 25;
WriteConsoleOutputCharacterA(hSet, PrintfBuff, strlen(PrintfBuff), coord, &bytes);
coord.X = 0;
coord.Y = 1;
memset(PrintfBuff, 0, sizeof PrintfBuff);
strcpy(PrintfBuff, "功能:");
WriteConsoleOutputCharacterA(hSet, PrintfBuff, strlen(PrintfBuff), coord, &bytes);
coord.X = 5;
memset(PrintfBuff, 0, sizeof PrintfBuff);
strcpy(PrintfBuff, "1暂停\\播放");
WriteConsoleOutputCharacterA(hSet, PrintfBuff, strlen(PrintfBuff), coord, &bytes);
coord.Y += 2;
memset(PrintfBuff, 0, sizeof PrintfBuff);
sprintf(PrintfBuff, "2上一首(%s)", PreSong);
WriteConsoleOutputCharacterA(hSet, PrintfBuff, strlen(PrintfBuff), coord, &bytes);
coord.Y += 2;
memset(PrintfBuff, 0, sizeof PrintfBuff);
sprintf(PrintfBuff, "3下一首(%s)", NextSong);
WriteConsoleOutputCharacterA(hSet, PrintfBuff, strlen(PrintfBuff), coord, &bytes);
coord.Y += 2;
memset(PrintfBuff, 0, sizeof PrintfBuff);
strcpy(PrintfBuff, "4停止");
WriteConsoleOutputCharacterA(hSet, PrintfBuff, strlen(PrintfBuff), coord, &bytes);
coord.Y += 2;
memset(PrintfBuff, 0, sizeof PrintfBuff);
strcpy(PrintfBuff, "5添加到我的歌单");
WriteConsoleOutputCharacterA(hSet, PrintfBuff, strlen(PrintfBuff), coord, &bytes);
coord.Y += 2;
memset(PrintfBuff, 0, sizeof PrintfBuff);
strcpy(PrintfBuff, "6增加音量");
WriteConsoleOutputCharacterA(hSet, PrintfBuff, strlen(PrintfBuff), coord, &bytes);
coord.Y += 2;
memset(PrintfBuff, 0, sizeof PrintfBuff);
strcpy(PrintfBuff, "7减小音量");
WriteConsoleOutputCharacterA(hSet, PrintfBuff, strlen(PrintfBuff), coord, &bytes);
coord.Y += 2;
memset(PrintfBuff, 0, sizeof PrintfBuff);
strcpy(PrintfBuff, "8快进5s");
WriteConsoleOutputCharacterA(hSet, PrintfBuff, strlen(PrintfBuff), coord, &bytes);
coord.Y += 2;
memset(PrintfBuff, 0, sizeof PrintfBuff);
strcpy(PrintfBuff, "9快退5s");
WriteConsoleOutputCharacterA(hSet, PrintfBuff, strlen(PrintfBuff), coord, &bytes);
coord.Y += 2;
memset(PrintfBuff, 0, sizeof PrintfBuff);
strcpy(PrintfBuff, "0返回主菜单");
WriteConsoleOutputCharacterA(hSet, PrintfBuff, strlen(PrintfBuff), coord, &bytes);
memset(PrintfBuff, 0, sizeof PrintfBuff);
//sprintf(PrintfBuff, "请输入命令指令(注意命令指令是否有效)");
coord.X = 1;
coord.Y = 27;
//WriteConsoleOutputCharacterA(hSet, PrintfBuff, LENGTH, coord, &bytes);
SetConsoleActiveScreenBuffer(hSet);
BuffSwitch = !BuffSwitch;
hSet = hOut[BuffSwitch];
}
//设置光标位置
static void SetPos(int x, int y)
{
COORD point = { x , y }; //光标要设置的位置x,y
SetConsoleCursorPosition(handle_out, point); //设置光标位置
}
//显示音符
void note(int x, int y)
{
SetPos(x, y++); printf(" ******************************");
SetPos(x, y++); printf(" ******************************");
SetPos(x, y++); printf(" *** ***");
SetPos(x, y++); printf(" *** ***");
SetPos(x, y++); printf(" ******************************");
SetPos(x, y++); printf(" ******************************");
SetPos(x, y++); printf(" *** ***");
SetPos(x, y++); printf(" *** ***");
SetPos(x, y++); printf(" *** ***");
SetPos(x, y++); printf(" *** ***");
SetPos(x, y++); printf(" *** ***");
SetPos(x, y++); printf(" *** ***");
SetPos(x, y++); printf(" *** ***");
SetPos(x, y++); printf(" **** ***");
SetPos(x, y++); printf(" ***** ***");
SetPos(x, y++); printf(" ****** *****");
SetPos(x, y++); printf(" **** ******");
SetPos(x, y++); printf(" ****");
}
//打印主菜单
void PrintMainMenu()
{
note(45, 1);
SetPos(0, 20);
printf("***********************************************************************************************************************\n");
printf("* *\n");
printf("* 主菜单 *\n");
printf("* *\n");
printf("* 1.所有歌曲 *\n");
printf("* *\n");
printf("* 2.我的歌单 *\n");
printf("* *\n");
printf("* 3.正在播放 *\n");
printf("* *\n");
printf("* 4.播放模式 *\n");
printf("* *\n");
printf("* 0.退出 *\n");
printf("* *\n");
printf("***********************************************************************************************************************");
printf("\n");
}
//进入曲库
int AllMusic(void)
{
system("cls");
ListFlag = 0;
note(45, 1);
SetPos(0, 20);
printf("***********************************************************************************************************************\n");
for (int i = 1; i <= MusicNumber * 2 + 3; i++)
printf("* *\n");
printf("***********************************************************************************************************************\n");
SetPos(58, 22);
printf("所有歌曲");
SetPos(57, 24);
int m = 24;
for (int i = 1; i <= MusicNumber; i++)
{
printf("%d ", i);
char temps[100] = { NULL };
strncpy(temps, alls[i], strlen(alls[i]) - 4);
printf("%s", temps);
m += 2;
SetPos(57, m);
}
log_record(INFO, "LINE: %d print all songs", __LINE__ - 2);
printf("\n1选择播放歌曲 2返回主菜单\n");
int tempFlag = -1;
do
{
printf("请输入命令指令(注意命令指令是否有效)\n");
while (1 != scanf("%d", &tempFlag))
{
printf("输入命令格式错误,请重新输入\n");
log_record(ERROR_, "LINE: %d input format error", __LINE__ - 3);
while (getchar() != '\n'); //读取所有缓冲区错误命令格式,包括回车
}
} while (tempFlag < 1 || tempFlag > 2);
if (1 == tempFlag) //选择播放歌曲,从绝对id播放
{
printf("请输入歌曲id\n");
scanf("%d", &IDInput);
strcpy(BroName, alls[IDInput]);
Play();
log_record(INFO, "LINE: %d play music named %s ", __LINE__ - 1, BroName);
log_record(INFO, "LINE: %d read lyric named %s ", __LINE__ - 2, BroName);
return IDInput;
}
else return -1;
}
//我的歌单
int MyMusic(void)
{
SetPos(58, 22);
printf("我的歌单");
SetPos(57, 24);
printf("1选择歌单 ");
SetPos(57, 26);
printf("2创建歌单 ");
SetPos(57, 28);
printf("3删除歌单 ");
SetPos(57, 30);
printf("0返回主菜单");
SetPos(57, 32);
printf(" ");
printf("\n\n\n");
int temp = -1;
do
{
printf("请输入命令指令(注意命令指令是否有效)\n");
while (1 != scanf("%d", &temp))
{
printf("输入命令格式错误,请重新输入\n");
log_record(ERROR_, "input format error", __LINE__ - 3);
while (getchar() != '\n'); //读取所有缓冲区错误命令格式,包括回车
}
} while (temp < 0 || temp > 3);
int m = 22;
switch (temp)
{
case 1:
system("cls");
note(45, 1);
SetPos(0, 20);
printf("***********************************************************************************************************************\n");
for (int i = 1; i <= ListNumber * 2 + 3; i++)
printf("* *\n");
printf("***********************************************************************************************************************\n");
SetPos(57, 22);
for (int i = 0; i <= ListNumber; i++)
{
if (!i)
{
SetPos(58, 22);
printf("歌单名称"); //
SetPos(57, 24);
m += 2;
continue;
}
printf("%d ", i);
char templ[100] = { NULL };
strncpy(templ, listname[i], strlen(listname[i]) - 4);
printf("%s", templ);
m += 2;
SetPos(57, m);
}
m += 2;
SetPos(0, m);
if (!ListNumber) printf("未创建歌单!\n");
else
{
printf("请输入歌单编号\n");
scanf("%d", &ListFlag);
log_record(INFO, "LINE: %d open %d list sucessfully", __LINE__ - 1, ListFlag);
}
break;
case 2:
printf("请输入歌单名称\n");
scanf("%s", listname[++ListNumber]);
log_record(INFO, "LINE: %d create new list named %s", __LINE__ - 1, listname[ListNumber]);
strcat(listname[ListNumber], ".txt");
SaveList(ListNumber);
log_record(INFO, "LINE: %d save list numbered %d and named %s sucessfully", __LINE__ - 1, ListNumber, listname[ListNumber]);
printf("创建成功,即将返回主菜单");
Sleep(2000);
return -1;
break;
case 3:
system("cls");
note(45, 1);
SetPos(0, 20);
printf("***********************************************************************************************************************\n");
for (int i = 1; i <= ListNumber * 2 + 3; i++)
printf("* *\n");
printf("***********************************************************************************************************************\n");
SetPos(57, 22);
for (int i = 0; i <= ListNumber; i++)
{
if (!i)
{
SetPos(58, 22);
printf("歌单名称"); //
SetPos(57, 24);
m += 2;
continue;
}
printf("%d ", i);
char templ[100] = { NULL };
strncpy(templ, listname[i], strlen(listname[i]) - 4);
printf("%s", templ);
m += 2;
SetPos(57, m);
}
m += 2;
SetPos(0, m);
printf("请输入待删除的歌单编号:\n");
int deletelist;
scanf("%d", &deletelist);
if (!DeleteList(deletelist))
{
log_record(INFO, "LINE: %d list %d had been removed!", __LINE__ - 2, deletelist);
printf("删除歌单成功!\n");
printf("即将返回菜单\n");
Sleep(1000);
}
else
{
log_record(ERROR_, "LINE: %d list %d removed failed!", __LINE__ - 10, deletelist);
printf("删除歌单失败!\n");
printf("即将返回菜单\n");
Sleep(1000);
}
return -1;
break;
case 0:
return -1;
}
if (!ListNumber) return -1;
MARK2: PrintList(ListFlag);
log_record(INFO, "LINE: %d print %d list named %s", __LINE__ - 1, ListFlag, listname[ListFlag]);
printf("1选择播放歌曲 2从歌单删除歌曲 0返回主菜单\n");
int tempFlag = -1;
do
{
printf("请输入命令指令(注意命令指令是否有效)\n");
while (1 != scanf("%d", &tempFlag))
{
printf("输入命令格式错误,请重新输入\n");
log_record(ERROR_, "LINE: %d input format error", __LINE__ - 3);
while (getchar() != '\n'); //读取所有缓冲区错误命令格式,包括回车
}
} while (tempFlag < 0 || tempFlag > 2);
if (1 == tempFlag)
{
if (!ListSize[ListFlag])
{
printf("歌单为空,即将返回主菜单");
Sleep(1000);
return -1;
}
do {
printf("请输入歌曲id(注意歌曲id是否有效)\n");
while (1 != scanf("%d", &IDInput))
{
printf("输入歌曲id错误,请重新输入\n");
log_record(ERROR_, "LINE: %d input format error", __LINE__ - 3);
while (getchar() != '\n'); //读取所有缓冲区错误命令格式,包括回车
}
} while (IDInput < 1 || IDInput > ListSize[ListFlag]); //用户输入合法
strcpy(BroName, GetName(IDInput, ListFlag));
Play();
log_record(INFO, "LINE: %d play music named %s ", __LINE__ - 1, BroName);
log_record(INFO, "LINE: %d read lyric named %s ", __LINE__ - 2, BroName);
return IDInput;
}
else if (2 == tempFlag)
{
int MusicNum = -1;
do {
printf("请输入待删除的歌曲id(注意歌曲id是否有效)\n");
while (1 != scanf("%d", &MusicNum))
{
printf("输入歌曲id错误,请重新输入\n");
log_record(ERROR_, "LINE: %d input format error", __LINE__ - 3);
while (getchar() != '\n'); //读取所有缓冲区错误命令格式,包括回车
}
} while (MusicNum < 1 || MusicNum > ListSize[ListFlag]); // 默认用户输入合法
deleteone(MusicNum, ListFlag);
printf("删除成功\n");
system("cls");
SaveList(ListFlag);
goto MARK2;
return -1;
}
else return -1;
}
//显示播放界面功能
void PrintDisPlayMenu(char* PreSong, char* NextSong)
{
SetPos(0, 1), printf("功能:");
SetPos(5, 1), printf("1暂停\\播放");
SetPos(5, 3), printf("2上一首(%s)", PreSong);
SetPos(5, 5), printf("3下一首(%s)", NextSong);
SetPos(5, 7), printf("4停止");
SetPos(5, 9), printf("5添加到我的歌单");
SetPos(5, 11), printf("6增加音量");
SetPos(5, 13), printf("7减小音量");
SetPos(5, 15), printf("8快进5s");
SetPos(5, 17), printf("9快退5s");
SetPos(5, 19), printf("0返回主菜单");
SetPos(5, 21), printf("请输入命令指令(注意命令指令是否有效)");
}
//播放界面
void Display()
{
int music_alltime = 0;
char time_all[100];
char time_cur[100];
mciSendString("status mymusic length", time_all, 100, NULL);
music_alltime = atoi(time_all); //int总时 6位数
music_alltime /= 1000; //多少秒
while (!kbhit())
{
mciSendString("status mymusic position", time_cur, 100, NULL);
int music_timeing = atoi(time_cur);
int music_timeing_ms = music_timeing;
music_timeing /= 1000;
//上一首歌歌名和下一首歌歌名
char PreSong[100] = { NULL }, NextSong[100] = { NULL };
strncpy(PreSong, GetPrev(), strlen(GetPrev()) - 4);
strncpy(NextSong, GetNext(), strlen(GetNext()) - 4);
DoubleBuffSet(music_timeing_ms, music_alltime, music_timeing, PreSong, NextSong);
Sleep(50); // 延迟50毫秒,让CPU休息一下
//结合播放模式和时长
if (music_timeing == music_alltime - 2)
{
char CtrlCmd[CTRLCMD_SIZE];
char pathindex[100] = "C://CloudMusic//";
switch (ModeFlag) //在当前歌单操作
{
case 1://顺序播放