Implementation of music player based on madplay library linux Application Programming

Keywords: Linux Programming

Implementation of music player based on madplay library linux Application Programming
The system can realize the following functions: play, pause, continue, stop, last song, next song, volume adjustment and exit.
Using madplay library to realize a simple music player, which can play local audio files and has basic music operation and processing functions

Main menu rendering:

Function corresponding function

Filename function name return value type function description
main.c main() int main function of the program
remind() int prompt interface
Song * create "mp3" list () void
Mp3 ﹐ continue ﹐ pause() void to pause and continue playing songs
Playing method of MP3 play void songs
Mp3 start void to play songs
Mp3 stop void to stop songs
Mp3 next void to switch songs to the next one
Mp3 ﹐ prior void ﹐ to switch songs to the previous one


The code is as follows:

/**********************************************************
   Fliename          :    new_mp3.c
   Author            :        group
   Data              :       2017.6
   Description         :   It can play music in sequence, switch the previous song, the next song, pause and continue, and adjust the volume
   Function list        :    remind()
   **********************************************************/

    #include <time.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <string.h>
    #include <signal.h>
    #include <sys/stat.h>
    #include <sys/ipc.h>
    #include <sys/wait.h>
    #include <sys/time.h>
    #include <sys/shm.h>
    #include <sys/types.h>
    #include <linux/input.h>
    #include <linux/soundcard.h>

    #define START 1
    #define PAUSE 0
 
     int iLeft = 60;                                  
    int iRight = 20;

    int remind()                                    
    {
        printf("\033[31m************Mp3 Player************\033[0m\n");
        printf("\033[31m________remind information________\033[0m\n");
        printf("\033[31m              1.play              \033[0m\n");
        printf("\033[31m          2.pasue/continue        \033[0m\n");
                printf("\033[31m               3.stop             \033[0m\n");
        printf("\033[31m               4.next             \033[0m\n");
        printf("\033[31m               5.prev             \033[0m\n");
        printf("\033[31m             6.voice up           \033[0m\n");
        printf("\033[31m             7.voice down         \033[0m\n");
                printf("\033[31m               8.exit             \033[0m\n");
        printf("\033[31m__________________________________ \033[0m\n");
    }
    struct song                                        
    {
        pid_t id;
        char music_name[50];                              
        struct song *next;                                  
        struct song *prior;                                 
    };

    struct song *p1,*p2,*head;
    struct song *shm_buf;                               
    int shmid;                                           
    int NameLen;
    pid_t ChildPid;                                       
    int ContinuePauseFlag = PAUSE;                         
    struct tm *ptr;
    time_t t;                                              
    void *shm_memory = (void *)0;


    /************************************************************************
    Function         :    song *create_mp3_list
    Description       :    Create a song list
    Call             :    fopen(),strcpy()
    Call    by       :    main()
    ***********************************************************************/

    struct song *create_mp3_list(void)
    {
        FILE *list_fd;                                
        size_t size;                                 
        size_t len;
        struct song *p;
        char *line = NULL;
        system("ls /home/amor/Desktop/music/song>/home/amor/Desktop/music/song_list.txt"); 
    
    
    if((list_fd = fopen("/home/amor/Desktop/music/song_list.txt","r")) == NULL)
        {
            perror("open song list failure!\n");
            exit(1);
        }
        

        
        p1 = (struct song *)malloc(sizeof(struct song));
        
        if(NULL == p1)                             
        {
            printf("malloc error!\n");
            exit(0);
        }

               
        size = getline(&line,&len,list_fd);

        /* Copy line to the music "name" of p1 */
        strncpy(p1->music_name,line,strlen(line));
        
        head = p1;
        
        
        while((size =getline(&line,&len,list_fd)) != -1)   
        {
            p2 = p1;
            p1 = (struct song *)malloc(sizeof(struct song));
            strncpy(p1->music_name,line,strlen(line));
            p1->music_name[strlen(line)]='\0';
            p2->next = p1;
            p1->prior = p2;
        }

        p1->next = head;
        head->prior = p1;
        p1 = NULL;
        p2 = NULL;
        return head;                              
    }

    /********************************************************************
    Function         :    mp3_continue_pause
    Description       :    Pause and resume songs
    Call             :    memcpy()
    Call    by       :    main()
**********************************************************************/
    void mp3_continue_pause(struct song *CurrentMusic)
    {
        pid_t GrandPid;
        memcpy(&GrandPid,&CurrentMusic->id,sizeof(pid_t));

        if(ContinuePauseFlag == 0)                       
        {
            kill(ChildPid,SIGSTOP);                         
            kill(GrandPid,SIGSTOP);                          
            ContinuePauseFlag = 1;                       
            printf("\033[34mThe song is pause!\033[0m\n");
        }

        else if(ContinuePauseFlag == 1)
        {
            kill(ChildPid,SIGCONT);
            kill(GrandPid,SIGCONT);
            ContinuePauseFlag = 0;
                        printf("\033[34mThe song is continue!\033[0m\n");
        }
    }

    /*****************************************************************
    Function         :    mp3_play
    Description       :    How to play songs
    Call             :    strcat()
    Call    by             :    main()                :   
*****************************************************************/

    void mp3_play(struct song *CurrentMusic)
    {
    char *dir;
    char dirname[40] ="/home/amor/Desktop/music/song/";   
        strcat(dirname,CurrentMusic->music_name);         
        dir=dirname;              
    NameLen = strlen(dir);
        dirname[NameLen - 1] = '\0';             
           execlp("madplay","madplay","-q",dirname,NULL);
           perror("execlp");
    }

    /****************************************************************
     Function         :    mp3_start
    Description        :    Play songs
    Call              :    mp3_play()
    Call    by        :    main()
     ****************************************************************/        
    void mp3_start(struct song *CurrentMusic)
    {
        pid_t GrandsonPid;
        struct song *nextmusic;
        memcpy(shm_buf,CurrentMusic,sizeof(struct song)); /*Copy current music to shmbuf*/
        ChildPid = fork();                  

        if(ChildPid < 0)    
        {
            perror("fork failure!\n");
            exit(1);
        }
        
        if(ChildPid == 0)                       
        {
            while(1)
            {
                GrandsonPid = fork();           
    
                if(GrandsonPid < 0)
                {
                    perror("create Grandson process fail!\n");
                    exit(1);
                }
    
                else 
                {
                    if(GrandsonPid == 0)            
                    {
                        t = time(NULL);
                        printf("\033[34mPrior song is: %s\033[0m\n",(shm_buf->prior)->music_name);
                        printf("\033[34mNow is playing: %s\033[0m\n",shm_buf->music_name);
                        printf("\033[34mNext song is: %s\033[0m\n",(shm_buf->next)->music_name);
                        mp3_play(shm_buf);       
                    }

                    else                           
                    {
                    shm_memory = shmat(shmid,(void *)0,0);
    
                        if(shm_memory == (void *)-1)
                        {
                            perror("shmat fail!\n");
                            exit(1);
                        }
    
                        shm_buf = (struct song *)shm_memory;
                        memcpy(&shm_buf->id,&GrandsonPid,sizeof(pid_t));  
                        wait(&GrandsonPid);          

                }
            }
        }
    }
}
    /*****************************************************************
    Function        :    mp3_stop
    Description      :    Stop the song
    Call            :    memcpy()
    Call    by      :    main()
    *****************************************************************/

    void mp3_stop(struct song *CurrentMusic)
    {
        pid_t GrandPid;
        memcpy(&GrandPid,&CurrentMusic->id,sizeof(pid_t));
        kill(ChildPid,SIGKILL);
        kill(GrandPid,SIGKILL);
    }

    /********************************************************************
    Function        :    mp3_next
    Description      :   Switch songs to next
    Call            :   memcpy()
    Call    by      :   main()
    **********************************************************************/

    void mp3_next(struct song *CurrentMusic)
    {
        struct song *NextMusic;
        pid_t GrandPid;
        memcpy(&GrandPid,&CurrentMusic->id,sizeof(pid_t));
        kill(ChildPid,SIGKILL);
        kill(GrandPid,SIGKILL);
        NextMusic = CurrentMusic;
        NextMusic = NextMusic->next;
        wait(NULL);
        printf("\033[34mthe next song is %s\033[0m\n",NextMusic->music_name);
        mp3_start(NextMusic);
    }
/*******************************************************************
    Function          :    mp3_prior
    Description        :   Switch songs to previous
    Call              :   memcpy()
    Call    by        :   main()
    ********************************************************************/
    void mp3_prior(struct song *CurrentMusic)
    {
    pid_t GrandPid;
    struct song *PriorMusic;
    memcpy(&GrandPid,&CurrentMusic->id,sizeof(pid_t));
    kill(ChildPid,SIGKILL);
    kill(GrandPid,SIGKILL);
    PriorMusic = CurrentMusic;
    PriorMusic = PriorMusic->prior;
    wait(NULL);
    mp3_start(PriorMusic);
    }

    int main(void)
    {
    shmid = shmget(IPC_PRIVATE,sizeof(struct song),0666 | IPC_CREAT);
        int MIX_FD= open("/dev/mixer", O_WRONLY);    
    if(shmid < 0)
    {
    printf("create messge queue fail!\n");
    exit(1);
    }
    
    shm_memory = shmat(shmid,(void *)0,0);
    
    if(shm_memory == (void *)-1)
    {
    perror("shmat fail!\n");
    exit(1);
    }
    shm_buf = (struct song *)shm_memory;
    head = create_mp3_list();                                   
    while(1)
    {
        char current_buttons[6];
        int a;
        remind();
        printf("\033[34mplease enter your choose:\033[0m\n");
scanf("%d",&a); 
        int iLevel;
    switch(a)
    {
        case 1:
            mp3_start(head);
            break;
        case 2:
            mp3_continue_pause(shm_buf);
            break;
        case 3:
            mp3_stop(shm_buf);
            printf("\033[34mnow song stops\033[0m\n");    
            break;
        case 4:
            mp3_next(shm_buf);
            break;
        case 5:
            mp3_prior(shm_buf);
            break;
        case 6:
            iLeft += 5;                 
            iLevel = (iRight << 8) + iLeft;
            ioctl(MIX_FD, MIXER_WRITE(SOUND_MIXER_VOLUME), &iLevel);
            printf("\033[34mVolume has up\033[0m\n");
            break;
        case 7:
            iLeft -= 5;                    
            iLevel = (iRight << 8) + iLeft;
            ioctl(MIX_FD, MIXER_WRITE(SOUND_MIXER_VOLUME), &iLevel);
            printf("\033[34mVolume has down\033[0m\n");
            break;
        case 8:
            kill(0,SIGKILL);
                break;
}}
    return 0;
    }

 

36 original articles published, 83 praised, 260000 visitors+
Private letter follow

Posted by jdashca on Fri, 13 Mar 2020 08:59:01 -0700