c语言MP3播放器

来源:互联网 发布:通用电气公司人事知乎 编辑:程序博客网 时间:2024/05/15 05:39
#include<stdio.h>#include<string.h>#include<stdlib.h>#include <afx.h>//CString的头文件#include<time.h>//使用当前时钟做种子#include <windows.h>#include<mmsystem.h>#include <conio.h>#pragma comment(lib,"WINMM.LIB")void jiemian(){printf("|-----------------------------------------------------|\n");printf("|1-->顺序播放      **2-->随机播放   **3-->单曲循环播放|\n");printf("|-----------------------------------------------------|\n");printf("|                                                     |\n");printf("|4-->切换指定歌曲  **5-->暂停       **6-->播放        |\n");printf("|-----------------------------------------------------|\n");printf("|                                                     |\n");printf("|7-->上一首        **8-->下一首     **9-->停止        |\n");printf("|-----------------------------------------------------|\n");printf("|                                                     |\n");printf("|10-->快进        **11-->后退                         |\n");printf("|-----------------------------------------------------|\n");}/*void play(int n){MCIERROR re;    //FILE *fp;CString fe;char song[30];printf("请输入歌名: ");    fflush(stdin);    gets(song);//输入曲目名称    fe.Format("open %s alias beatit",song);     re=mciSendString(TEXT(fe.GetBuffer(fe.GetLength())),0,0,0);//TEXT是宏变量,alias不能少    printf("\n");mciSendString(TEXT("play beatit"),0,0,0);//mciSendString("play D:\\KwDownload\\song\\3.mp3",NULL,0,NULL);Sleep(10000000);}*//*BOOL CMyDlg::OnInitDialog(){CDialog::OnInitDialog();CFile file1("歌曲列表.lw",CFile::modeReadWrite|CFile::modeCreate);if(file1.m_hFile==NuLL){CFile file1("歌曲列表。lw",CFile::modeReadWrite|CFile::modeNoTruncate);}else{while(mpd.title!=NULL){file1.Read(&mpd,sizeof(mpd));int nRow=m_list.InsertItem(1,mpd,title);m_list.SetItemText(nRow,1,mpd,arti);if(mpd.head && "TAG"){CString str="MP3";m_list.SetItemText(nRow,2,str);}}file.Close;}}*//*void pause(int n){mciSendString("pause movie",NULL,0,NULL);}*/int NUMBER()//获取歌曲总数量{int i=0;FILE *fp;//指针指向文件;char SongName[1024];fp=fopen("列表.txt","r+");if(fp==NULL){printf("open failed\n");exit(-1);}while(!feof(fp)){fgets(SongName,1024,fp);//将文件内的歌名逐行输入到SongName中储存;i++;}return i;}void PlayList()//打开播放列表{FILE *fp;char SongName[1024];int i=0;fp=fopen("列表.txt","r+");if(fp==NULL){printf("open failed\n");exit(-1);}while(!feof(fp)){fgets(SongName,1024,fp);i++;printf("%d--%s",i,SongName);}printf("\n");}void PlayNext(int count,int gn)//播放下一首{MCIERROR def;//对函数进行声明,通过API函数mciSendString传递给MCICString str;//一种数据类型,能简化MFC的很多操作 网上代码复制,看不懂;FILE *fp;long hour;void PlayPast(int count,int gn);//播放上一首void playsernumber();//指定序号播放void RandomPlay();//随机播放int i=0,len,k,s;char song[1024]={0},sheet[1024];hour=clock();s=NUMBER();//将歌曲总数量赋值给s,方便下面使用;fp=fopen("列表.txt","r+");//以只读的方式打开文本文档if(fp==NULL){printf("open failed!");exit(-1);}while(!feof(fp)){//j++;fgets(sheet,1024,fp);i++;if(i==count)break;}len=strlen(sheet);//将整首歌名的长度赋值给len,方便下面将歌名传递给数组song[];fflush(stdin);//清除读写缓冲区,需要立即把输出缓冲区的数据stdin进行物理写入;if(i==s)//判断是不是最后一首歌,最后一行歌名如果有回车,就把回车去掉;{for(k=0;k<len;k++){song[k]=sheet[k];}}else if(i<s){for(k=0;k<len-1;k++)song[k]=sheet[k];}str.Format("open %s alias Mysong",song);//将歌曲打开def=mciSendString(TEXT(str.GetBuffer(str.GetLength())),0,0,0);//这个函数是为一个CString对象重新获取其内部字符缓冲区的指针,//返回的LPTSTR为非const的,从而允许直接修改CString中的内容。mciSendString(TEXT("play Mysong"),0,0,0);//进行歌曲播放;这三句都是网上复制的代码//Sleep(240000);while(scanf("%d",&gn)!=EOF){if(gn==5)//歌曲暂停{mciSendString(TEXT("pause Mysong"),0,0,0);printf("successful\n");}else if(gn==6)//歌曲继续播放{mciSendString(TEXT("resume Mysong"),0,0,0);printf("successful\n");}else if(gn==7)//播放上一首{mciSendString (TEXT("close Mysong"), 0, 0, 0);count=count-1;if(count<1)count=s;printf("successful\n");PlayPast(count,gn);//播放上一首}else if(gn==8)//播放下一首{mciSendString (TEXT("close Mysong"), 0, 0, 0);//先将正在播放的歌曲关闭count=count+1;if(count>s)count=1;printf("successful\n");PlayNext(count,gn);}else if(gn==9)//停止播放{mciSendString(TEXT("stop Mysong"), 0, 0, 0);mciSendString (TEXT("close Mysong"), 0, 0, 0);printf("successful\n");}else if(gn==4)//指定歌曲进行播放{mciSendString (TEXT("close Mysong"), 0, 0, 0);printf("successful\n");playsernumber();}else if(gn==3)//单曲循环播放{mciSendString(TEXT("play Mysong repeat"), 0, 0, 0);printf("successful\n");}else if(gn==2)//随机播放{mciSendString (TEXT("close Mysong"), 0, 0, 0);RandomPlay();}else if(gn==10)//快进{hour=hour+5000;str.Format("seek Mysong to %ld",hour);mciSendString(TEXT(str.GetBuffer(str.GetLength())),0,0,0);mciSendString(TEXT("play Mysong"),0,0,0);printf("successful\n");}else if(gn==11)//快退{hour=hour-5000;if(hour<0)hour=0;str.Format("seek Mysong to %ld",hour);mciSendString(TEXT(str.GetBuffer(str.GetLength())),0,0,0);mciSendString(TEXT("play Mysong"),0,0,0);printf("successful\n");}}Sleep(300000);}void PlayPast(int count,int gn)//播放上一首{MCIERROR def;//对函数进行声明,通过API函数mciSendString传递给MCICString str;//一种数据类型,能简化MFC的很多操作 网上代码复制,看不懂;FILE *fp;int i=0,len,k,s;long hour;char song[1024]={0},sheet[1024];void PlayNext(int count,int gn);//播放下一首void playsernumber();//指定序号播放void RandomPlay();//随机播放hour=clock();//c++里面的计时函数s=NUMBER();//将歌曲总数量赋值给s,方便下面使用;fp=fopen("列表.txt","r+");if(fp==NULL){printf("open failed!");exit(-1);}while(!feof(fp)){//j++;fgets(sheet,1024,fp);i++;if(i==count)break;}len=strlen(sheet);//将整首歌名的长度赋值给len,方便下面将歌名传递给数组song[];fflush(stdin);//清除读写缓冲区,需要立即把输出缓冲区的数据stdin进行物理写入if(i==s){for(k=0;k<len;k++){song[k]=sheet[k];}}else if(i<s){for(k=0;k<len-1;k++)song[k]=sheet[k];}//printf("%s %s",song,sheet);//song[k+1]='\0';str.Format("open %s alias Mysong",song);//将歌曲打开def=mciSendString(TEXT(str.GetBuffer(str.GetLength())),0,0,0);//这个函数是为一个CString对象重新获取其内部字符缓冲区的指针,//返回的LPTSTR为非const的,从而允许直接修改CString中的内容。mciSendString(TEXT("play Mysong"),0,0,0);//歌曲播放//Sleep(240000);while(scanf("%d",&gn)!=EOF){if(gn==5)//暂停{mciSendString(TEXT("pause Mysong"),0,0,0);printf("successful\n");}else if(gn==6)//暂停后播放{mciSendString(TEXT("resume Mysong"),0,0,0);printf("successful\n");}else if(gn==7)//播放上一首{mciSendString (TEXT("close Mysong"), 0, 0, 0);count=count-1;if(count<1)count=s;printf("successful\n");PlayPast(count,gn);//播放上一首}else if(gn==8)//播放下一首{mciSendString (TEXT("close Mysong"), 0, 0, 0);count=count+1;if(count>s)count=1;printf("successful\n");PlayNext(count,gn);}else if(gn==9)//停止播放{mciSendString(TEXT("stop Mysong"), 0, 0, 0);mciSendString (TEXT("close Mysong"), 0, 0, 0);printf("successful\n");}else if(gn==4)//随机播放{mciSendString (TEXT("close Mysong"), 0, 0, 0);printf("successful\n");playsernumber();}else if(gn==3)//单曲循环{mciSendString(TEXT("play Mysong repeat"), 0, 0, 0);printf("successful\n");}else if(gn==2)//随机播放{mciSendString (TEXT("close Mysong"), 0, 0, 0);RandomPlay();}else if(gn==10)//快进{hour=hour+5000;str.Format("seek Mysong to %ld",hour);mciSendString(TEXT(str.GetBuffer(str.GetLength())),0,0,0);mciSendString(TEXT("play Mysong"),0,0,0);printf("successful\n");}else if(gn==11)//快退{hour=hour-5000;if(hour<0)hour=0;str.Format("seek Mysong to %ld",hour);mciSendString(TEXT(str.GetBuffer(str.GetLength())),0,0,0);mciSendString(TEXT("play Mysong"),0,0,0);printf("successful\n");}}Sleep(300000);}void playsernumber()    //指定序号播放{MCIERROR def;FILE *fp;long hour;char song[1024]={0};char sheet[1024];   //歌单;CString str;int j=0,k=0,count=0,num,len,gn,n;void RandomPlay();//随机播放hour=clock();n=NUMBER();//printf("%d\n",n);fp=fopen("列表.txt","r+");printf("请输入歌前面指定数字进行播放\n");scanf("%d",&num);if(fp==NULL){printf("open failed!");exit(-1);}while(!feof(fp)){//j++;fgets(sheet,1024,fp);count++;if(count==num)break;}len=strlen(sheet);fflush(stdin);if(count==n){for(k=0;k<len;k++){song[k]=sheet[k];}}else if(count<n){for(k=0;k<len-1;k++)song[k]=sheet[k];}str.Format("open %s alias Mysong",song);def=mciSendString(TEXT(str.GetBuffer(str.GetLength())),0,0,0);mciSendString(TEXT("play Mysong"),0,0,0);while(scanf("%d",&gn)!=EOF){if(gn==5){mciSendString(TEXT("pause Mysong"),0,0,0);printf("successful\n");}else if(gn==6){mciSendString(TEXT("resume Mysong"),0,0,0);printf("successful\n");}else if(gn==7){mciSendString (TEXT("close Mysong"), 0, 0, 0);count=count-1;if(count<1)count=n;printf("successful\n");PlayPast(count,gn);//播放上一首}else if(gn==8){mciSendString (TEXT("close Mysong"), 0, 0, 0);count=count+1;if(count>n)count=1;printf("successful\n");PlayNext(count,gn);}else if(gn==9){mciSendString(TEXT("stop Mysong"), 0, 0, 0);mciSendString (TEXT("close Mysong"), 0, 0, 0);printf("successful\n");}else if(gn==4){mciSendString (TEXT("close Mysong"), 0, 0, 0);printf("successful\n");playsernumber();}else if(gn==3){mciSendString(TEXT("play Mysong repeat"), 0, 0, 0);printf("successful\n");}else if(gn==2){mciSendString (TEXT("close Mysong"), 0, 0, 0);RandomPlay();}else if(gn==10)//快进{hour=hour+5000;str.Format("seek Mysong to %ld",hour);mciSendString(TEXT(str.GetBuffer(str.GetLength())),0,0,0);mciSendString(TEXT("play Mysong"),0,0,0);printf("successful\n");}else if(gn==11)//快退{hour=hour-5000;if(hour<0)hour=0;str.Format("seek Mysong to %ld",hour);mciSendString(TEXT(str.GetBuffer(str.GetLength())),0,0,0);mciSendString(TEXT("play Mysong"),0,0,0);printf("successful\n");}}Sleep(300000);}void RandomPlay()    //随机播放{MCIERROR def;FILE *fp;long hour;char song[1024]={0};char sheet[1024];   //歌单;CString str;int j=0,k=0,count=0,num,len,gn,n;hour=clock();n=NUMBER();//printf("%d\n",n);fp=fopen("列表.txt","r+");srand( (unsigned)time( NULL ) );            num=rand()%n+1;if(fp==NULL){printf("open failed!");exit(-1);}while(!feof(fp)){//j++;fgets(sheet,1024,fp);count++;if(count==num)break;}len=strlen(sheet);fflush(stdin);if(count==n){for(k=0;k<len;k++){song[k]=sheet[k];}}else if(count<n){for(k=0;k<len-1;k++)song[k]=sheet[k];}str.Format("open %s alias Mysong",song);def=mciSendString(TEXT(str.GetBuffer(str.GetLength())),0,0,0);mciSendString(TEXT("play Mysong"),0,0,0);while(scanf("%d",&gn)!=EOF){if(gn==5){mciSendString(TEXT("pause Mysong"),0,0,0);printf("successful\n");}else if(gn==6){mciSendString(TEXT("resume Mysong"),0,0,0);printf("successful\n");}else if(gn==7){mciSendString (TEXT("close Mysong"), 0, 0, 0);count=count-1;if(count<1)count=n;printf("successful\n");PlayPast(count,gn);//播放上一首}else if(gn==8){mciSendString (TEXT("close Mysong"), 0, 0, 0);count=count+1;if(count>n)count=1;printf("successful\n");PlayNext(count,gn);}else if(gn==9){mciSendString(TEXT("stop Mysong"), 0, 0, 0);mciSendString (TEXT("close Mysong"), 0, 0, 0);printf("successful\n");}else if(gn==4){mciSendString (TEXT("close Mysong"), 0, 0, 0);printf("successful\n");playsernumber();}else if(gn==3){mciSendString(TEXT("play Mysong repeat"), 0, 0, 0);printf("successful\n");}else if(gn==2){mciSendString (TEXT("close Mysong"), 0, 0, 0);RandomPlay();}else if(gn==10)//快进{hour=hour+5000;str.Format("seek Mysong to %ld",hour);mciSendString(TEXT(str.GetBuffer(str.GetLength())),0,0,0);mciSendString(TEXT("play Mysong"),0,0,0);printf("successful\n");}else if(gn==11)//快退{hour=hour-5000;if(hour<0)hour=0;str.Format("seek Mysong to %ld",hour);mciSendString(TEXT(str.GetBuffer(str.GetLength())),0,0,0);mciSendString(TEXT("play Mysong"),0,0,0);printf("successful\n");}}Sleep(300000);}void SequentialPlay()    //顺序播放{MCIERROR re;FILE *fp;long here;char song[1024]={0};char sheet[1024],slen[30]={0};   //歌单;long int llen;CString fe;int j=0,k=0,count=0,num,len,n;void RandomPlay();//随机播放void playsernumber();  //指定序号播放here=clock();n=NUMBER();//printf("%d\n",n);fp=fopen("列表.txt","r+");//printf("请输入歌前面指定数字进行播放\n");num=1;if(fp==NULL){printf("open failed!");exit(-1);}while(!feof(fp)){//j++;fgets(sheet,1024,fp);count++;if(count==num)break;}len=strlen(sheet);fflush(stdin);if(count==n){for(k=0;k<len;k++){song[k]=sheet[k];}}else if(count<n){for(k=0;k<len-1;k++)song[k]=sheet[k];}fe.Format("open %s alias Mysong",song);re=mciSendString(TEXT(fe.GetBuffer(fe.GetLength())),0,0,0);mciSendString(TEXT("play Mysong"),0,0,0);mciSendString(TEXT("status Mysong length"),slen,255,0);//网上参考    llen=strtol(slen,0,0);Sleep(llen);    mciSendString (TEXT("close Mysong"), 0, 0, 0);while(n--){ num++;if(fp==NULL){printf("open failed!");exit(-1);}while(!feof(fp)){//j++;fgets(sheet,1024,fp);count++;if(count==num)break;}len=strlen(sheet);fflush(stdin);if(count==n){for(k=0;k<len;k++){song[k]=sheet[k];}}else if(count<n){for(k=0;k<len-1;k++)song[k]=sheet[k];}fe.Format("open %s alias Mysong",song);re=mciSendString(TEXT(fe.GetBuffer(fe.GetLength())),0,0,0);mciSendString(TEXT("play Mysong"),0,0,0);mciSendString(TEXT("status Mysong length"),slen,255,0);//网上参考    llen=strtol(slen,0,0);Sleep(llen);    mciSendString (TEXT("close Mysong"), 0, 0, 0);}/*while(scanf("%d",&gn)!=EOF){if(gn==5){mciSendString(TEXT("pause Mysong"),0,0,0);printf("successful\n");}else if(gn==6){mciSendString(TEXT("resume Mysong"),0,0,0);printf("successful\n");}else if(gn==7){mciSendString (TEXT("close Mysong"), 0, 0, 0);count=count-1;if(count<1)count=n;printf("successful\n");PlayPast(count,gn);//播放上一首}else if(gn==8){mciSendString (TEXT("close Mysong"), 0, 0, 0);count=count+1;if(count>n)count=1;printf("successful\n");PlayNext(count,gn);}else if(gn==9){mciSendString(TEXT("stop Mysong"), 0, 0, 0);mciSendString (TEXT("close Mysong"), 0, 0, 0);printf("successful\n");}else if(gn==4){mciSendString (TEXT("close Mysong"), 0, 0, 0);printf("successful\n");playsernumber();}else if(gn==3){mciSendString(TEXT("play Mysong repeat"), 0, 0, 0);printf("successful\n");}else if(gn==2){mciSendString (TEXT("close Mysong"), 0, 0, 0);RandomPlay();}else if(gn==10)//快进{here=here+50000;fe.Format("seek Mysong to %ld",here);mciSendString(TEXT(fe.GetBuffer(fe.GetLength())),0,0,0);mciSendString(TEXT("play Mysong"),0,0,0);printf("successful\n");}else if(gn==11)//快退{here=here-50000;if(here<0)here=0;fe.Format("seek Mysong to %ld",here);mciSendString(TEXT(fe.GetBuffer(fe.GetLength())),0,0,0);mciSendString(TEXT("play Mysong"),0,0,0);printf("successful\n");}}Sleep(300000);*/}int main(){int n;int num;jiemian();printf("\n");NUMBER();PlayList();num=NUMBER();while(scanf("%d",&n)!=EOF){if(n==4){jiemian();playsernumber();}else if(n==2){mciSendString (TEXT("close Mysong"), 0, 0, 0);RandomPlay();}else if(n==1){jiemian();SequentialPlay();}/*else if(n==1){mciSendString (TEXT("close Mysong"), 0, 0, 0);SequentialPlay();}*/}return 0;}/*#include<stdio.h>#include <windows.h>#include <conio.h> int main(){    printf("如果你三秒钟之内什么也不输入,我就输出-1。\n");    Sleep(3000);    if(!_kbhit())        printf("-1\n");    else        printf("输入了%c", getchar());    return 0;}*//*#include <stdlib.h>#include <stdio.h>#include <time.h> void main( ){int i;srand( (unsigned)time( NULL ) );         for( i = 0; i < 10;i++ )            printf( "%d\n", rand()%100+1);}*/

歌曲必须与编程语言放在同一个文件夹内   歌曲名需要自己手动输入到文本文档中
0 0
原创粉丝点击