Thread: C program MP3 Player

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    10

    C program MP3 Player

    Trying to make a command line mp3 player in C.
    Having trouble adding and playing and deleting songs into my library on the program. Wondering if anything has done any similar project or any examples we can use, as this is hard, hahaha.

    Thanks in advance

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you're essentially not able to do anything of your project? [or is there anything beside Adding, Playing and Deleting MP3's that an MP3 player does (and I'm not counting having a graphical pulsing display moving along the musig)]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Try for something slightly easier if you're not up to the task.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    I can play one song, as long as I type in the location of the file on disk, but in case 3 i am opening my notepad file as its it should play them song from the list, but it is not. any idea why?

    this is the code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    FILE * pFile;
    
    #define ARRAYSIZE 100
    #define SONGNAME 25
    #define SONGARTIST 25
    #define SONGLENGTH 25
    #define TRACK_NUMBER
    
    void func1(void);
    void func2(void);
    void func3(void);
    void func4(void);
    void delete_song(void);
    void func6(void);
    
    typedef struct Mp3rec
    {
            char name[SONGNAME];
            char artist[SONGARTIST];
            char length[SONGLENGTH];
            char tname[TRACK_NUMBER];
    };
    
    
    int main (void)
    {
        
        
        
        int menuchoice=0;
        //fp= fopen("mp3_list.txt", "r" "W" "a"); either to add or read a text file.
        do
        {
          printf ("********************************************\n");
          printf ("************************************\n");
          printf ("*********** MP3 Player **********\n");
          printf ("********************************************");
          printf("\n1: Play File");   
          printf("\n2: Show Library");
          printf("\n3: Add Record");
          printf("\n4: Edit/Modify ");
          printf("\n5: Delete Record");
          printf("\n6: Quit");
          
          printf("\n\nEnter Choice From 1-6: ");
          scanf("&#37;d", &menuchoice);
    
          switch (menuchoice)
          {
                 case 1:
                        func1();
                        break;
                 case 2:
                        func2();
                        break;
                 case 3:
                        func3();
                        break;
                 case 4:
                        func4();
                        break;
                        
                 case 5:delete_song();
                        break;              
                                            
                 case 6:  
                        break;     
                        
                 default:
                         printf("\nInvalid Choice: 1-4 Only Please");
                         }
                         
                         }while(menuchoice!=4);
                         system("pause");
    }
    
    void func1(void)
    {
         printf("\n\n Play File\n\n");
         printf ("********************************************\n");
         printf ("************************************\n");
         printf ("*********** **********\n");
         printf ("********************************************");
         {
        char commandarray[ARRAYSIZE];
        
        char mp3filename[ARRAYSIZE] ="C:\\mplayer\\Robin S - Show me Love.mp3";
        
        sprintf(commandarray, "C:\\mplayer\\mplayer.exe \"%s\"", mp3filename);
        
        printf("\nAttempting to Run Command \"%s\"...\n\n", commandarray);
        
        system(commandarray);
        
        int menuchoice=0;
        switch (menuchoice)
        printf("\n1: Back To Menu");
        printf("\n5: Quit");
       
        printf("\nDone.\n\n");
        
        
       
          
        system("pause");
        
    }    
    }
    
    void func2(void)
    {
         printf("\n\n Show Library\n\n");
    }                                                                                              
    
    void func3(void)
    {
        
         printf("\n\n Add Record/Song\n\n");
         fopen("ListofSongs.txt","r+");
                           
         printf("\n\n Add Record\n\n");
         
             
    }        
    
    void func4(void)
    { 
         printf("\n\n Edit/Modify the Current Song\n\n");    
    }    
        
    void delete_song(void)
    {
         printf("\n\n Delete File\n\n");
         
     int i;
     int track_number;
    
    
         printf("Please Enter the track number that you wish to delete:");
         fflush(stdin);
         scanf("%d", &track_number);
    
    }
    
         //for( Track_Number = Track_Number ; Track_Number <=Total Songs ; Track_Number ++)
    
    
    void func6(void)
    {
        // break;
    }

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Function names like func1()..... Are you trying to obfuscate your code?

    Quote Originally Posted by kjs6thf View Post
    in case 3 i am opening my notepad file as its it should play them song from the list, but it is not. any idea why?
    Yes, I do have an idea of why it doesn't play the song from a list because you wrote absolutely nothing of importance in case 3, but you expect us to write it for you.

    Code:
    void func3(void)
    {
        
         printf("\n\n Add Record/Song\n\n");
         fopen("ListofSongs.txt","r+");
                           
         printf("\n\n Add Record\n\n");
         
             
    }
    Do you honestly want us to believe that you expect this to do anything?

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    Quote Originally Posted by MacGyver View Post
    Function names like func1()..... Are you trying to obfuscate your code?



    Yes, I do have an idea of why it doesn't play the song from a list because you wrote absolutely nothing of importance in case 3, but you expect us to write it for you.

    Code:
    void func3(void)
    {
        
         printf("\n\n Add Record/Song\n\n");
         fopen("ListofSongs.txt","r+");
                           
         printf("\n\n Add Record\n\n");
         
             
    }
    Do you honestly want us to believe that you expect this to do anything?

    Don't try and jump to conclusions, I have only just started to program for a month, so I am not going to be brilliant am I? I ask for advice, and I get given sarcastic comments back. Thanks.

    Maybe without the criticism can you say what is wrong with case 3 please?
    Surely- fopen("ListofSongs.txt","r+"); should open the file and read it line by line....

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by kjs6thf View Post
    Don't try and jump to conclusions, I have only just started to program for a month, so I am not going to be brilliant am I? I ask for advice, and I get given sarcastic comments back. Thanks.

    Maybe without the criticism can you say what is wrong with case 3 please?
    Surely- fopen("ListofSongs.txt","r+"); should open the file and read it line by line....
    Surely - fopen will open a file. The reading it line by line, well, that's done by something else. (You might think fread would read a file, and you would be right, but not in this context.) Look up file i/o and how to get text out of a file.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    First, congrats on your first effort - it's ambitious for a new programmer.

    I have a question and a suggestion. Your program reads filenames (songs) from a text file list. So when you say "delete a file", do you mean delete that song's title from the text file, or do you *actually* want to delete the song itself?

    My suggestion would be to simply delete the song's title from the list of songs, and leave the actual song, alone.

    Modifying a song, tell me what you intend for that to do? My understanding of songs on a computer are limited (very), but there is a great deal of programming involved with modifying music. Not something I'd recommend for a beginner to try and program.

    Pay no mind to MacGyver's sarcasm, we get a great many requests by "daisy picker's" who want everything coded up for their homework or project, and show little willingness to work at making the program, themselves.

    I don't count you among that group, and I think this is an interesting program to take on.

    Oh! Nearly forgot. Check out the tutorial on reading a text file, line by line. You'll want a while or for loop, to make the reading of the lines of text, happen.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by kjs6thf View Post
    Don't try and jump to conclusions, I have only just started to program for a month, so I am not going to be brilliant am I? I ask for advice, and I get given sarcastic comments back. Thanks.
    Since you seem to be insulted by my response, let me explain something. There is a huge difference between simply being a user and being a programmer. If you want to use a program, you can get around many or even most Windows programs without ever having to read any manual or readme or doing any work outside of fiddling with the program and seeing what happens. It's even the method I like to use, since many GUI applications function in similar ways.

    If you try that approach with programming, your efforts will only frustrate those you ask for help as well as yourself. By all means, test things out, but don't write random things and expect them to work. What's even worse is that even if random things appear to work, very often that might be just what happens on the surface and what really is happening is not what you expected nor desired. Especially don't ask people why random things don't work. When you write code, you are telling the computer to do very specific things. Specific directions require logic that randomness will most likely not generate for you.

    Quote Originally Posted by kjs6thf View Post
    Maybe without the criticism can you say what is wrong with case 3 please?
    Surely- fopen("ListofSongs.txt","r+"); should open the file and read it line by line....
    Sure, you open the file, forget to save the resulting FILE * that is returned, and then do nothing.

    There is no reason to think fopen() reads a file line by line, and to be completely honest, it sounds like you pulled it out of nowhere as an excuse to get us to tell you exactly how to do the reading. Is that the case? I have no idea, and frankly, I don't care. Maybe you used to know how to read from a file and forgot. It's your program, and it's your project anyway. It doesn't bother me.

    My irritation is that it appears you have not done any real research into how file processing or even fopen() works and just slapped it together and wanted us to do it. If you're interested in becoming an expert, you need to be able to better understand what your problem is before you ask others for help. This means you should have read a basic file tutorial first, and also had basic knowledge on how to use fopen(), fclose(), and hopefully some file input functions. It's considered common courtesy to the rest of us who you've asked to help you out, to at least know what you're talking about and to make an honest effort.

    So back to what I said in my past post:

    • Your function names are bad. Naming something generic like function() is a way to totally confuse yourself and others viewing your code. Things like that are usually meant to obfuscate code. If you pick meaningful names like playSong() or readSongList(), then it would be easier to figure out what is going on.
    • Your usage of fopen() is wrong. You need to declare a FILE * and set it equal to the result of fopen() and then test it to see if that is NULL. If it isn't, then your file is successfully opened. Go ahead and read from it. After that, remember to call fclose() on the FILE *.
    • I don't know where you got that fopen() does reading, but it's horribly wrong. If someone told you this or you read it from a website, consider ignoring most of what they say regarding C. If you made it up, then see what I wrote above regarding random code.
    Last edited by MacGyver; 12-04-2007 at 06:04 PM.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    Quote Originally Posted by tabstop View Post
    Surely - fopen will open a file. The reading it line by line, well, that's done by something else. (You might think fread would read a file, and you would be right, but not in this context.) Look up file i/o and how to get text out of a file.
    Cheers, I'll have a look.
    Quote Originally Posted by Adak View Post
    First, congrats on your first effort - it's ambitious for a new programmer.

    I have a question and a suggestion. Your program reads filenames (songs) from a text file list. So when you say "delete a file", do you mean delete that song's title from the text file, or do you *actually* want to delete the song itself?

    My suggestion would be to simply delete the song's title from the list of songs, and leave the actual song, alone.

    Modifying a song, tell me what you intend for that to do? My understanding of songs on a computer are limited (very), but there is a great deal of programming involved with modifying music. Not something I'd recommend for a beginner to try and program.

    Pay no mind to MacGyver's sarcasm, we get a great many requests by "daisy picker's" who want everything coded up for their homework or project, and show little willingness to work at making the program, themselves.

    I don't count you among that group, and I think this is an interesting program to take on.

    Oh! Nearly forgot. Check out the tutorial on reading a text file, line by line. You'll want a while or for loop, to make the reading of the lines of text, happen.
    Yes I just want the song to be deleted from the text file rather than off the computer/hard drive it self. The Problem basically is, if you look in case 1, I have typed in Robin S - Show me love.mp3 that is the name of the song that will play in command prompt if I Run it. So to change what song I would like to listen too, I have to physically change the file name every time, so I need to write the location of the song in a text file and get to read it from there and be able to choose what song I want to listen too out of say 40 which are in the file.
    Thanks again.

    Quote Originally Posted by MacGyver View Post
    Since you seem to be insulted by my response, let me explain something. There is a huge difference between simply being a user and being a programmer. If you want to use a program, you can get around many or even most Windows programs without ever having to read any manual or readme or doing any work outside of fiddling with the program and seeing what happens. It's even the method I like to use, since many GUI applications function in similar ways.

    If you try that approach with programming, your efforts will only frustrate those you ask for help as well as yourself. By all means, test things out, but don't write random things and expect them to work. What's even worse is that even if random things appear to work, very often that might be just what happens on the surface and what really is happening is not what you expected nor desired. Especially don't ask people why random things don't work. When you write code, you are telling the computer to do very specific things. Specific directions require logic that randomness will most likely not generate for you.



    Sure, you open the file, forget to save the resulting FILE * that is returned, and then do nothing.

    There is no reason to think fopen() reads a file line by line, and to be completely honest, it sounds like you pulled it out of nowhere as an excuse to get us to tell you exactly how to do the reading. Is that the case? I have no idea, and frankly, I don't care. Maybe you used to know how to read from a file and forgot. It's your program, and it's your project anyway. It doesn't bother me.

    My irritation is that it appears you have not done any real research into how file processing or even fopen() works and just slapped it together and wanted us to do it. If you're interested in becoming an expert, you need to be able to better understand what your problem is before you ask others for help. This means you should have read a basic file tutorial first, and also had basic knowledge on how to use fopen(), fclose(), and hopefully some file input functions. It's considered common courtesy to the rest of us who you've asked to help you out, to at least know what you're talking about and to make an honest effort.

    So back to what I said in my past post:

    • Your function names are bad. Naming something generic like function() is a way to totally confuse yourself and others viewing your code. Things like that are usually meant to obfuscate code. If you pick meaningful names like playSong() or readSongList(), then it would be easier to figure out what is going on.
    • Your usage of fopen() is wrong. You need to declare a FILE * and set it equal to the result of fopen() and then test it to see if that is NULL. If it isn't, then your file is successfully opened. Go ahead and read from it. After that, remember to call fclose() on the FILE *.
    • I don't know where you got that fopen() does reading, but it's horribly wrong. If someone told you this or you read it from a website, consider ignoring most of what they say regarding C. If you made it up, then see what I wrote above regarding random code.
    In no way was I trying to insult anyone in any shape or form as a matter of fact. I merely stated that I am requiring some help and where I am/have gone wrong with my current code.

    Thanks for the advice on function names and so-forth. Regarding File * haven't I declared that at the top under the "#includes"?.....

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by kjs6thf View Post
    Yes I just want the song to be deleted from the text file rather than off the computer/hard drive it self. The Problem basically is, if you look in case 1, I have typed in Robin S - Show me love.mp3 that is the name of the song that will play in command prompt if I Run it. So to change what song I would like to listen too, I have to physically change the file name every time, so I need to write the location of the song in a text file and get to read it from there and be able to choose what song I want to listen too out of say 40 which are in the file.
    Thanks again.
    Then you could do what others do when they have binary files. Create and use an index which keeps track of where every filename is in the file.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes I just want the song to be deleted from the text file rather than off the computer/hard drive it self. The Problem basically is, if you look in case 1, I have typed in Robin S - Show me love.mp3 that is the name of the song that will play in command prompt if I Run it. So to change what song I would like to listen too, I have to physically change the file name every time, so I need to write the location of the song in a text file and get to read it from there and be able to choose what song I want to listen too out of say 40 which are in the file.
    Thanks again.
    Oh the hell with changing the text file every time!

    We're programmers!

    Playlists are usually selectable by using a mouse, or typing the name of the file, but I like using numbers better because it leaves my fingers on the keyboard.

    1. Robin S - "Love ..."
    2. Koko Taylor - "Loving ..."
    3. etc.

    "Please Enter Your Song's Number: "

    So enter 1 to play Robin, and 2 for Koko, etc.

    For a console program, this is not difficult to code up. For a full Windows app - well, I don't program for Windows.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    Quote Originally Posted by Elysia View Post
    Then you could do what others do when they have binary files. Create and use an index which keeps track of where every filename is in the file.
    Thanks matey.

    Quote Originally Posted by Adak View Post
    Oh the hell with changing the text file every time!

    We're programmers!

    Playlists are usually selectable by using a mouse, or typing the name of the file, but I like using numbers better because it leaves my fingers on the keyboard.

    1. Robin S - "Love ..."
    2. Koko Taylor - "Loving ..."
    3. etc.

    "Please Enter Your Song's Number: "

    So enter 1 to play Robin, and 2 for Koko, etc.

    For a console program, this is not difficult to code up. For a full Windows app - well, I don't program for Windows.
    That's exactly what I want to do Adak, problem is I have no idea.!!!!!!

    In my text file it's currenty like this:

    1
    Robin S
    Show me Love
    Dance
    3:48
    c://stevendocuments//mymusic//dance//robin s - show me love.mp3

    its the same layout for every song.

    Any ideas?

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also, since we're "programmers," you should keep the playlist in memory. That way you won't have a hard time searching the file to find the right position in the text file to delete a song.
    Or you could use some type of registry storage. In case you don't want to reinvent the wheel, registry exists in Windows. But then again, perhaps it's a little overcourse right now.
    And C:// is wrong in Windows. Unless you're using Linux?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    Quote Originally Posted by Elysia View Post
    Also, since we're "programmers," you should keep the playlist in memory. That way you won't have a hard time searching the file to find the right position in the text file to delete a song.
    Or you could use some type of registry storage. In case you don't want to reinvent the wheel, registry exists in Windows. But then again, perhaps it's a little overcourse right now.
    And C:// is wrong in Windows. Unless you're using Linux?
    thanks much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Screw with this mp3 player
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-20-2004, 01:17 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM