您的位置:首页 > 运维架构 > Linux

Linux下的MP3播放程序

2017-10-01 23:10 197 查看
运行环境Centos 6.5

C语言

实现mp3的大部分功能。

/***********************************************************************
> File Name: mp3.c
> Author: YangKun
> Mail: yangkungetit@163.com
> Created Time: Sat 30 Sep 2017 11:24:28 PM PDT
************************************************************************/
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<dirent.h>
#include <sys/types.h>

typedef struct _node
{
char name[256];
struct _node* prev;
struct _node* next;
}node;

node* head = NULL;
node* cur  =NULL;
int first = 1;

void play()
{
if(first==1)
{
first=0;
char buf[1024]={};
sprintf(buf,"madplay -o wav:- %s 2>/dev/null | aplay 2>/dev/null  &",cur->name);
system(buf);
}
else if(first==0)
{
system("killall -19 madplay");
}
}

void continue_play()
{
system("killall -18 madplay");
}

void Insert_list(char* name)
{
node* p=(node*)malloc(sizeof(node));
memset(p,0x00,sizeof(node));
strcpy(p->name,name);

if(head==NULL)
{
p->prev=p->next=p;
head = p;
cur=p;
return ;
}
p->prev=head->prev;
p->next=head;
head->prev->next=p;
head->prev=p;
}

void prev()
{
system("killall -9 madplay");
cur = cur->prev;
first = 1;
play();
}

void next()
{
system("killall -9 madplay");
cur = cur->next;
first = 1;
play();
}

void show_list()
{
node* p=head;
aff7

if(p==NULL)
{
printf("no music in list\n");
exit(0);
}
int i=1;
do
{
printf("%d-->%s\n",i++,p->name);
p=p->next;
}while(p!=head);
}

void near_music()
{
printf("Currently playing:%s\n",cur->name);
printf("Last Piece Of Song:%s\n",cur->prev->name);
printf("Next Song:%s\n",cur->next->name);
}

void add_music()
{
DIR *pdir=opendir("/mp3");
if ( pdir == NULL )
perror("opendir"),exit(1);
struct dirent *p=NULL;
char pathname[256]={};
while((p=readdir(pdir))!=NULL)
{
if(p->d_name[0]=='.')
continue;
memset(pathname,0x00,sizeof(pathname));
sprintf(pathname,"/mp3/%s",p->d_name);
Insert_list(pathname);
}
closedir(pdir);
}
int menu()
{
int i;
printf("\n+-------------------------------------------------------------+\n");
printf("|                                                             | \n");
printf("|                 Welcome to MP3 PLAYER                       | \n");
printf("|                                                             | \n");
printf("|                      1.Play\\stop                            | \n");
printf("|                      2.continue                             | \n");
printf("|                      3.prev song                            | \n");
printf("|                      4.next song                            | \n");
printf("|                                                             | \n");
printf("|_____________________________________________________________| \n");
printf("choose->");
scanf("%d",&i);
return i;
}

void start_mp3()
{
int input = 1;
do
{
show_list();
switch(menu())
{
case 1:
play();
break;
case 2:
continue_play();
break;
case 3:
prev();
break;
case 4:
next();
break;
default:
printf("again\n");
break;
}
while(input)
{
near_music();
printf("^-^choose:");
scanf("%d",&input);
switch(input)
{
case 0:
break;
case 1:
play();
break;
case 2:
continue_play();
break;
case 3:
prev();
break;
case 4:
next();
break;
default:
printf("err operation\n");
}
}
}while(1);
}
int main()
{
add_music();
start_mp3();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: