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

所属分类: 软件编程 / C 语言 阅读数: 87
收藏 0 赞 0 分享

实例代码如下:

#include <stdio.h>
#include<dirent.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>


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++中四种对象生存期和作用域以及static的用法总结分析

以下是对C++中四种对象生存期和作用域以及static的用法进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++嵌套类与局部类详细解析

从作用域的角度看,嵌套类被隐藏在外围类之中,该类名只能在外围类中使用。如果在外围类之外的作用域使用该类名时,需要加名字限定
收藏 0 赞 0 分享

C++空类详解

以下是对C++中的空类进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++之友元:友元函数和友元类详解

友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元
收藏 0 赞 0 分享

C++中返回指向函数的指针示例

int (*ff(int)) (int *,int);表示:ff(int)是一个函数,带有一个int型的形参,该函数返回int (*) (int *,int),它是一个指向函数的指针,所指向的函数返回int型并带有两个分别是Int*和int型的形参
收藏 0 赞 0 分享

C数据结构之单链表详细示例分析

以下是对C语言中的单链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C数据结构之双链表详细示例分析

以下是对c语言中的双链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅析如何在c语言中调用Linux脚本

如何在c语言中调用Linux脚本呢?下面小编就为大家详细的介绍一下吧!需要的朋友可以过来参考下
收藏 0 赞 0 分享

深入解析unsigned int 和 int

以下是对unsigned int和int进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅谈C++中的string 类型占几个字节

本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节
收藏 0 赞 0 分享
查看更多