C语言音乐播放器实例代码

C语言音乐播放器实例代码,第1张

概述文章给大家分享了用C语言音乐播放器的实例代码,对此有需要的朋友参考学习下。

实例代码如下:

#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,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/pausen");

printf("2. nextn");

printf("3. prevn");

printf("4. stopn");

printf("5. exitn");

printf("**************************************n");

List_show();

int choose =4;

do{

printf(" > ");

scanf("%d",&choose);

if(choose>=0&&choose<=4)

break;

printf("choose invalIDn");

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("%sn",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语言音乐播放器实例代码全部内容,希望文章能够帮你解决C语言音乐播放器实例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1264875.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存