Thread: How to create a program that plays/saves an MP3 file.

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    28

    How to create a program that plays/saves an MP3 file.

    Hello!

    I am new to C programming and new to these forums. I finally worked out how you open a text file using fopen(), but cannot find a site that tells you how to open/save an MP3 file.

    Is there anyone who can help me with this? I would appreciate simple explanations as I would like to learn how this works.

    By the way, the site for this assignment gives a download called "mplayer" it's a command line player, and I'm not sure if this is needed for the MP3 to play.

    Thanks very much for your time!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I would assume you are just supposed to use an external command ("mplayer") via system() or exec(). I think mplayer maybe includes or at least uses the mpg123 libraries but you are probably not required to get into that.

    Of course, just using mplayer as a command will not actually allow you to open or save an .mp3, just play one.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    28
    Thanks! I've been looking around how to use this function, but I think that I need further knowledge in programming. However, I don't have the time to learn enough to know how to use it, so is anyone able to explain how to use the exec() function?

    It doesn't seem to have it in my book.

    Thanks again.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    "exec" actually refers to a group of functions (execv, execl, execvp, execlp, and so on). Probably the easiest one for what you want will be execlp.

    There are lots of docs and examples online, here's another:
    Code:
    execlp("cat","cat","somefile.txt",NULL);
    If you can't get it to work the way you want, post what you have tried.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    28
    Thanks! I managed to enter it with no errors, but could not work out what "cat" is for, so at the moment, it only has the name of the file I want to open and looked the function up on sites, but they seem to be aimed at people with good understanding of C already, so was unable to work it out.

    This is all I have done with it, and I found the library to include.

    Code:
    execlp("cat","cat","Kalimba.mp3",NULL);

  6. #6
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by m88g88 View Post
    Thanks! I managed to enter it with no errors, but could not work out what "cat" is for, so at the moment, it only has the name of the file I want to open and looked the function up on sites, but they seem to be aimed at people with good understanding of C already, so was unable to work it out.

    This is all I have done with it, and I found the library to include.

    Code:
    execlp("cat","cat","Kalimba.mp3",NULL);
    Cat was just an example for *nix-based systems...I think MK was hoping you could extrapolate that "cat" was the external program you were invoking and the rest were just parameters to your mp3 player. Replace "cat" with "mplayer" and see where it gets you. As far as reading/saving MP3 files, they are just binary files like anything else so open the file in binary mode and read/write as usual.

    If you want the simpler to understand yet less elegant solution, system("mplayer some_music_file.mp3"); will work too...it gives you less control over things but if you want a "feed the monkey, watch it poop" solution this would be it...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    28

    Smile

    Thank you so much! This works I'll experiment with it get to understand it, but it helps to understand how to do something when it's done first.

    I am really grateful!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM