C語言音樂播放器實(shí)例代碼
更新時(shí)間:2018年07月06日 08:26:48 作者:lemon100521
文章給大家分享了用C語言音樂播放器的實(shí)例代碼,對此有需要的朋友參考學(xué)習(xí)下。
實(shí)例代碼如下:
#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;
}
實(shí)例效果圖片如下:

相關(guān)文章
使用單鏈表實(shí)現(xiàn)多項(xiàng)式計(jì)算示例
這篇文章主要介紹了使用單鏈表實(shí)現(xiàn)多項(xiàng)式計(jì)算示例,需要的朋友可以參考下2014-03-03
dev-c++創(chuàng)建lib(靜態(tài)鏈接庫)文件的實(shí)現(xiàn)步驟
本文主要介紹了dev-c++創(chuàng)建lib(靜態(tài)鏈接庫)文件的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
C++ boost 時(shí)間與日期處理詳細(xì)介紹
這篇文章主要介紹了C++ boost 時(shí)間與日期處理詳細(xì)介紹的相關(guān)資料,這里提供實(shí)例代碼,及實(shí)現(xiàn)效果,需要的朋友可以參考下2016-11-11

