音乐播放器命令行界面实现源码解析
cmd命令行方式的playlist及lyric实现同mpxUI,定时器功能播放歌曲控制等功能函数同mpxUI。不一样的地方为显示歌曲、歌词等。
完整代码见test.c文件https://github.com/menghun3/mpx/blob/master/mpxcmd/test.c
主要代码为
void main(int argc, char **argv) { int choice = 0; int ch; int ret = 0; char *p = NULL; ret = PlayListInit(); if (ret == -1) { printf("init play list error\n"); return; } //mpxInit(); print_note(); do { ch = getch(); choice = ch - '1'+ CUSTOMER_PLAYFROMFILE; switch (choice) { case CUSTOMER_PLAYFROMFILE: { char buf[MAX_PATH]= {0}; WCHAR songpath[MAX_PATH] = {0}; char lyricpath[MAX_PATH] = {0}; if (print_defaultplaylist() == -1) { printf("play list no item\n"); break; } printf("input number choice"); scanf("%d", &i); if (i == 0) { break; } i--; p = GetItemFromDefaultPlaylist(i); if (p == NULL) { break; } MakeLyricPathFromSongPath(p, lyricpath); LyricDestroy(); LyricInit(lyricpath); MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, p, strlen(p), songpath, MAX_PATH); system("cls"); printf("playing:%s\n", p); print_note(); EndTimeEvent(); mpxPlayFile(songpath); BeginTimeEvent(); //PrintLyric(); status = 1; } break; case CUSTOMER_PLAY: { mpxPlay(); BeginTimeEvent(); status = 1; } break; case CUSTOMER_STOP: { mpxStop(); EndTimeEvent(); status = 2; } break; case CUSTOMER_PAUSE: { mpxPause(); EndTimeEvent(); status = 3; } break; case CUSTOMER_PREV: { WCHAR songpath[MAX_PATH] = {0}; char lyricpath[MAX_PATH] = {0}; printf("\n"); if (i > 0) { i--; } p = GetItemFromDefaultPlaylist(i); if (p == NULL) { break; } MakeLyricPathFromSongPath(p, lyricpath); LyricDestroy(); LyricInit(lyricpath); MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, p, strlen(p), songpath, MAX_PATH); EndTimeEvent(); mpxPlayFile(songpath); BeginTimeEvent(); status = 1; } break; case CUSTOMER_NEXT: { WCHAR songpath[MAX_PATH] = {0}; char lyricpath[MAX_PATH] = {0}; printf("\n"); i++; p = GetItemFromDefaultPlaylist(i); if (p == NULL) { break; } MakeLyricPathFromSongPath(p, lyricpath); LyricDestroy(); LyricInit(lyricpath); MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, p, strlen(p), songpath, MAX_PATH); system("cls"); printf("playing:%s\n", p); print_note(); EndTimeEvent(); mpxPlayFile(songpath); BeginTimeEvent(); status = 1; } break; case CUSTOMER_PLAYLIST: { int plch; system("cls"); printf("%d:add\t", PLAYLIST_ADD); printf("%d:dir\t", PLAYLIST_ADDDIR); printf("%d:save\t", PLAYLIST_SAVEPL); printf("%d:exit\t", PLAYLIST_LAST); do { plch = getch(); plch = plch - '1' + PLAYLIST_ADD; switch (plch) { case PLAYLIST_ADD: { AddFile(); } break; case PLAYLIST_ADDDIR: { AddDirFile(); } break; case PLAYLIST_SAVEPL: { DefaultPlaylistSave(); printf("save play list\n"); } break; default: break; } } while (plch != PLAYLIST_LAST); } break; case CUSTOMER_SET: { int setch; system("cls"); printf("%d:vol plus\t", SET_VOLUME_PLUS); printf("%d:vol minus\t", SET_VOLUME_MINUS); printf("%d:exit", SET_LAST); do { setch = getch(); setch = setch - '1' + SET_VOLUME_PLUS; switch (setch) { case SET_VOLUME_PLUS: { setVolumePlus(); } break; case SET_VOLUME_MINUS: { setVolumeMinus(); } break; default: break; } } while (setch != SET_LAST); } break; case CUSTOMER_QUIT: { mpxDestroy(); return; } break; default: break; } system("cls"); printf("playing:%s\n", p); print_note(); } while (1); }
循环获取命令,根据命令清理屏幕及显示对应的命令选项。
音乐播放器通过最简单的方式实现音乐播放器主要的功能,可以认为是音乐播放器的框架,通过该框架可以扩展功能或者完善。其中为了实现方便简单,界面只使用了命令行方式和基于windows提供的界面组件实现,而不使用第三方界面开发包或者的windows提供的复杂的界面开发包。