Linux下的MP3播放程序

来源:互联网 发布:手机淘宝登录界面 编辑:程序博客网 时间:2024/06/06 04:25

运行环境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;    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;}