欢迎来到代码驿站!

C代码

当前位置:首页 > 软件编程 > C代码

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

时间:2020-10-27 15:44:03|栏目:C代码|点击:

实例代码如下:

#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++选择文件夹代码的封装

栏    目:C代码

下一篇:解析C++的线性表链式存储设计与相关的API实现

本文标题:C语言音乐播放器实例代码

本文地址:http://www.codeinn.net/misctech/16436.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有