From e0bef1b84d6138257db0e52598892f7cfc8c3188 Mon Sep 17 00:00:00 2001 From: jiacai_wang Date: Tue, 22 Jan 2019 17:56:14 +0800 Subject: [PATCH] almos done. except that volume control has not been implemented. --- PlayInterface.c | 62 ++++++++++++++ PlayInterface.h | 3 + fileHandler.c | 31 +++++++ fileHandler.h | 2 + main.c | 213 ++++++++++++++++++++++++++++++++++++++++++++++++ vol.c | 1 + vol.h | 1 + 7 files changed, 313 insertions(+) create mode 100644 PlayInterface.c create mode 100644 PlayInterface.h create mode 100644 fileHandler.c create mode 100644 fileHandler.h create mode 100644 main.c create mode 100644 vol.c create mode 100644 vol.h diff --git a/PlayInterface.c b/PlayInterface.c new file mode 100644 index 0000000..8246cee --- /dev/null +++ b/PlayInterface.c @@ -0,0 +1,62 @@ +#include +#include +#include // mci库头文件 +#pragma comment(lib, "winmm.lib") // 指定MCI库,mciSendString函数的定义在winmm.lib中 + + +// 播放当前曲,曲号由curno记录 +void play(const char *name) //播放音乐 +{ + char cmd[MAX_PATH] = {0}; + char pathname[MAX_PATH] = {0}; + + // 加路径 + sprintf(pathname, ".\\music\\%s", name); + // GetShortPathName用来转换短名,要求被转换的歌名必须能在指定目录下找到文件,否则转换失败。 + // 第一个参数:源文件名,第二个参数:目的文件名,第三个参数:目的数组长度。 + GetShortPathName(pathname, pathname, MAX_PATH); + + + // 定义发往MCI的命令,cmd指定命令存储的数组,后面参数跟printf()相同 + sprintf(cmd, "open %s", pathname); + // 发送命令。 + // 一、存储命令的数组首地址,二、接受MCI返回的信息,三、接受数组的长度,四、没用,NULL + mciSendString(cmd, "", 0, NULL); + sprintf(cmd, "play %s", pathname); + mciSendString(cmd, "", 0, NULL); +} + + +// 暂停当前曲,曲号由curno记录 +void pause(const char *name) // 暂停播放 +{ + char cmd[MAX_PATH] = {0}; + char pathname[MAX_PATH] = {0}; + + // 加路径 + sprintf(pathname, ".\\music\\%s", name); + // GetShortPathName用来转换短名,要求被转换的歌名必须能在指定目录下找到文件,否则转换失败。 + // 第一个参数:源文件名,第二个参数:目的文件名,第三个参数:目的数组长度。 + GetShortPathName(pathname, pathname, MAX_PATH); + + sprintf(cmd, "pause %s", pathname); + mciSendString(cmd,"",0,NULL); +} + +// 停止当前曲,曲号由curno记录 +void stop(const char *name) +{ + char cmd[MAX_PATH] = {0}; + char pathname[MAX_PATH] = {0}; + + // 加路径 + sprintf(pathname, ".\\music\\%s", name); + // GetShortPathName用来转换短名,要求被转换的歌名必须能在指定目录下找到文件,否则转换失败。 + // 第一个参数:源文件名,第二个参数:目的文件名,第三个参数:目的数组长度。 + GetShortPathName(pathname, pathname, MAX_PATH); + + sprintf(cmd, "stop %s", pathname); + mciSendString(cmd,"",0,NULL); + sprintf(cmd, "close %s", pathname); + mciSendString(cmd,"",0,NULL); +} \ No newline at end of file diff --git a/PlayInterface.h b/PlayInterface.h new file mode 100644 index 0000000..a0aecaf --- /dev/null +++ b/PlayInterface.h @@ -0,0 +1,3 @@ +extern void play(const char *name); +extern void pause(const char *name); +extern void stop(const char *name); diff --git a/fileHandler.c b/fileHandler.c new file mode 100644 index 0000000..01ef5cb --- /dev/null +++ b/fileHandler.c @@ -0,0 +1,31 @@ +#include +#include +#define MAX_SONG_NUM 200 + +extern char songs[MAX_SONG_NUM][50]; + +//返回path路径下的.mp3文件数 +int getPlaylist(char *path) +{ + long Handle = 0; + int songNum = 0; + struct _finddata_t FileInfo; + //if((Handle=_findfirst(".\\music\\*.mp3",&FileInfo))==-1L) + if((Handle=_findfirst(strcat(path,"\\*.mp3"),&FileInfo))==-1L) + { + printf("没有找到匹配的项目\n"); + } + else + { + strcpy(songs[++songNum],FileInfo.name); + + printf("%d: %s\n",songNum,FileInfo.name); + while(_findnext(Handle,&FileInfo)==0 && songNum +#include +#include +#include +#include "PlayInterface.h" +#include "fileHandler.h" + +#define MAX_SONG_NUM 200 + +char dir[20] = {0}; + +// 0:停止 +// 正数:正在播放 +// 负数:正在暂停 +int playing = 0; +int songNum = 0; +boolean isRndPlay = 0; +// 歌曲文件名数组 +char songs[MAX_SONG_NUM][50] = {""}; + +void showstatus() +{ + if(isRndPlay) + printf("随机播放模式,"); + else + printf("顺序播放模式,"); + if (playing == 0) + printf("当前为停止状态\n"); + else if (playing > 0) + printf("正在播放第%d曲:%s\n",playing,songs[playing]); + else + printf("正在暂停第%d曲:%s\n",-playing,songs[-playing]); +} + +void playctrl(void){ + while(1){ + system("cls"); + showstatus(); + printf("1. 播放/暂停\ + \n2. 停止\ + \n3. 上一曲\ + \n4. 下一曲\ + \n5. 随机/顺序播放\ + \n0. 退出\ + \n请输入操作类型:\n"); + switch (getch()) + { + case '0': + return; + break; + case '1': + if(playing == 0) //停止状态时按1播放第1曲 + { + playing = (1 + isRndPlay * rand())%songNum; + if(playing == 0) playing = songNum; + play(songs[playing]); + } + else if(playing > 0) //播放状态时暂停该曲 + { + playing = -playing; + pause(songs[-playing]); + } + else //暂停状态时播放该曲 + { + playing = -playing; + play(songs[playing]); + } + break; + case '2': + stop(songs[playing]); + playing = 0; + break; + case '3': + if(playing > 1 && playing <= songNum) + { + stop(songs[playing]); + //playing--; + playing = (playing - 1 + isRndPlay * rand()) % songNum; + if(playing == 0) playing = songNum; + play(songs[playing]); + } + else if(playing < -1 && playing >= -songNum) + { + stop(songs[-playing]); + //playing = -playing - 1; + playing = (-playing-1 +isRndPlay*rand())%songNum; + if(playing == 0) playing = songNum; + play(songs[playing]); + } + else if(abs(playing) == 1) //第1首上一曲为最后一首 + { + stop(songs[1]); + playing = (songNum + isRndPlay * rand())%songNum; + if(playing == 0) playing = songNum; + play((songs[playing])); + } + else + printf("歌曲序号不正确,你一定是个垃圾程序员:)\n"); + Sleep(1000); + while(kbhit()) getch(); //清除sleep期间的输入 + break; + case '4': + if(playing >= 0 && playing < songNum) + { + stop(songs[playing]); + //playing++; + playing = (playing + 1 + isRndPlay * rand()) % songNum; + if(playing == 0) playing = songNum; + play(songs[playing]); + } + else if(playing < 0 && playing >= -songNum) + { + stop(songs[-playing]); + //playing = -playing + 1; + playing = (-playing + 1 +isRndPlay*rand())%songNum; + if(playing == 0) playing = songNum; + play(songs[playing]); + } + else if(abs(playing) == songNum) //最后一首上一曲为第1首 + { + stop(songs[abs(songNum)]); + //playing = 1; + playing = (1 + isRndPlay * rand())%songNum; + if(playing == 0) playing = songNum; + play((songs[playing])); + } + else + printf("歌曲序号不正确,你一定是个垃圾程序员:)\n"); + Sleep(1000); + while(kbhit()) getch(); //清除sleep期间的输入 + break; + case '5': + isRndPlay = !isRndPlay; + break; + default: + printf("非法输入!\n"); + Sleep(1000); + while(kbhit()) getch(); //清除sleep期间的输入 + break; + } + } +} +void volctrl(void){ + while(1){ + system("cls"); + showstatus(); + printf("1. 音量增大\ + \n2. 音量减小\ + \n0. 退出\ + \n请输入操作类型:\n"); + switch(getch()) + { + case '0': + return; + break; + case '1': + printf("vol_up called\n"); + Sleep(1000); + break; + case '2': + printf("vol_down called\n"); + Sleep(1000); + break; + default: + printf("非法输入!\n"); + Sleep(1000); + while(kbhit()) getch(); //清除sleep期间的输入 + break; + } + } +} +void killme(void){ + exit(0); +} + +int main(int argc, char **argv){ + srand((unsigned)time(NULL)); //随机播放初始化 + + while(songNum == 0) + { + printf("请输入MP3媒体路径:\n"); + scanf("%s",&dir); + songNum = getPlaylist(&dir); + printf("path = %s,共%d首歌\n",&dir,songNum); + } + printf("请按任意键继续...\n"); + getch(); + while(1){ + system("cls"); + showstatus(); + printf("1. 播放控制\ + \n2. 音量调节\ + \n0. 退出\ + \n请输入操作类型:\n"); + switch (getch()) + { + case '0': + killme(); + case '1': + playctrl(); + break; + case '2': + volctrl(); + break; + default: + printf("非法输入!\n"); + Sleep(1000); + while(kbhit()) getch(); //清除sleep期间的输入 + break; + } + } + return 0; +} \ No newline at end of file diff --git a/vol.c b/vol.c new file mode 100644 index 0000000..625a494 --- /dev/null +++ b/vol.c @@ -0,0 +1 @@ +//"setaudio %s volume to %d", device,vol; diff --git a/vol.h b/vol.h new file mode 100644 index 0000000..3d3942f --- /dev/null +++ b/vol.h @@ -0,0 +1 @@ +extern void volup(); \ No newline at end of file