重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本文小编为大家详细介绍“C语言如何实现音乐播放器”,内容详细,步骤清晰,细节处理妥当,希望这篇“C语言如何实现音乐播放器”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
成都创新互联公司成都网站建设按需网站开发,是成都网站营销推广公司,为纯水机提供网站建设服务,有成熟的网站定制合作流程,提供网站定制设计服务:原型图制作、网站创意设计、前端HTML5制作、后台程序开发等。成都网站制作热线:13518219792实例代码如下:
#include#include #include #include #include typedef struct node_ node_t; struct node_{ char* name;//gequming node_t * prev; node_t * next; }; node_t *head = NULL; int first=1;//diyicibofnag node_t * cur =NULL;//dangqianbofang enum{STOP,PAUSE,PLAY}; int status = STOP; void List_init(void){ head = malloc(sizeof(node_t)); memset(head,0x00,sizeof(node_t)); head->next = head->prev=head; } void list_insert(const char* 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; } int menu(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(); int choose =4; do{ printf(" > "); scanf("%d",&choose); if(choose>=0&&choose<=4) break; printf("choose invalid\n"); while(getchar()!='\n'); }while(1); return choose; } void list_show(void){ node_t *p = head->next; while(p!=head){ printf("%s ",p->name); if(p==cur) printf("<<==cur"); printf("\n"); p = p->next; } } void load_music(const char * path){ DIR * pdir = opendir(path); if(pdir == NULL){ perror("opendir"); exit(1); } struct dirent * p = NULL; while((p=readdir(pdir))!=NULL){ if(p->d_name[0]=='.') continue; list_insert(p->d_name); } closedir(pdir); } void playPause(){ if(first==1){ char buf[1024] = {}; sprintf(buf,"madplay -o wav:- ./music/Music/%s 2> /dev/null | aplay 2>/dev/null &",cur->name); system(buf); first = 0; status = PLAY; }else{ if(status==PLAY){ system("killall -SIGSTOP aplay"); status = PAUSE; }else if(status==PAUSE){ system("killall -SIGCONT aplay"); status = PLAY; } } } void stop(){ system("killall -SIGKILL aplay"); first=1; } void next(){ stop(); cur = cur ->next; if(cur==head){ cur = cur->next; } playPause(); } void prev(){ stop(); cur = cur->prev; if(cur==head){ cur= cur->prev; } playPause(); } int main(int args,char * argv[]) { List_init(); load_music("./music/Music"); if(head->next!=head) cur = head->next; //printf("%s\n",cur->name); //list_show(); do{ int choose = menu(); switch(choose){ case 1: playPause(); break; case 2: next(); break; case 3: prev(); break; case 4: stop(); break; case 0: printf("thanks"); system("killall -SIGKILL aplay"); exit(0); break; default: break; //do nothing; } }while(1); return 0; }
实例效果图片如下:
读到这里,这篇“C语言如何实现音乐播放器”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注创新互联行业资讯频道。