C语言如何实现音乐播放器
C语言如何实现音乐播放器
本文小编为大家详细介绍“C语言如何实现音乐播放器”,内容详细,步骤清晰,细节处理妥当,希望这篇“C语言如何实现音乐播放器”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
实例代码如下:
#include<stdio.h>#include<dirent.h>#include<stdlib.h>#include<unistd.h>#include<string.h>typedefstructnode_node_t;structnode_{char*name;//gequmingnode_t*prev;node_t*next;};node_t*head=NULL;intfirst=1;//diyicibofnagnode_t*cur=NULL;//dangqianbofangenum{STOP,PAUSE,PLAY};intstatus=STOP;voidList_init(void){head=malloc(sizeof(node_t));memset(head,0x00,sizeof(node_t));head->next=head->prev=head;}voidlist_insert(constchar*name){node_t*p=malloc(sizeof(node_t));memset(p,0x00,sizeof(node_t));p->name=malloc(strlen(name)+1);strcpy(p->name,name);p->next=head->next;p->prev=head;head->next->prev=p;head->next=p;}intmenu(void){printf("*************menu************************\n");printf("1.play/pause\n");printf("2.next\n");printf("3.prev\n");printf("4.stop\n");printf("5.exit\n");printf("**************************************\n");list_show();intchoose=4;do{printf(">");scanf("%d",&choose);if(choose>=0&&choose<=4)break;printf("chooseinvalid\n");while(getchar()!='\n');}while(1);returnchoose;}voidlist_show(void){node_t*p=head->next;while(p!=head){printf("%s",p->name);if(p==cur)printf("<<==cur");printf("\n");p=p->next;}}voidload_music(constchar*path){DIR*pdir=opendir(path);if(pdir==NULL){perror("opendir");exit(1);}structdirent*p=NULL;while((p=readdir(pdir))!=NULL){if(p->d_name[0]=='.')continue;list_insert(p->d_name);}closedir(pdir);}voidplayPause(){if(first==1){charbuf[1024]={};sprintf(buf,"madplay-owav:-./music/Music/%s2>/dev/null|aplay2>/dev/null&",cur->name);system(buf);first=0;status=PLAY;}else{if(status==PLAY){system("killall-SIGSTOPaplay");status=PAUSE;}elseif(status==PAUSE){system("killall-SIGCONTaplay");status=PLAY;}}}voidstop(){system("killall-SIGKILLaplay");first=1;}voidnext(){stop();cur=cur->next;if(cur==head){cur=cur->next;}playPause();}voidprev(){stop();cur=cur->prev;if(cur==head){cur=cur->prev;}playPause();}intmain(intargs,char*argv[]){List_init();load_music("./music/Music");if(head->next!=head)cur=head->next;//printf("%s\n",cur->name);//list_show();do{intchoose=menu();switch(choose){case1:playPause();break;case2:next();break;case3:prev();break;case4:stop();break;case0:printf("thanks");system("killall-SIGKILLaplay");exit(0);break;default:break;//donothing;}}while(1);return0;}
实例效果图片如下:
读到这里,这篇“C语言如何实现音乐播放器”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注恰卡编程网行业资讯频道。