How to play MP3 files in C?

c mp3
Sam picture Sam · Jan 9, 2009 · Viewed 98.3k times · Source

I'm looking for the easiest way to play a MP3 file in C. I am looking for either a library, in which I could just call the function on the filename, or an executable that will just run and quit. Please suggest.

Answer

Gabriele D'Antona picture Gabriele D'Antona · Jan 9, 2009

Using FMOD (cross platform), this should be as simple as this:

#include <conio.h>
#include "inc/fmod.h"

FSOUND_SAMPLE* handle;

int main ()
{
   // init FMOD sound system
   FSOUND_Init (44100, 32, 0);

   // load and play mp3
   handle=FSOUND_Sample_Load (0,"my.mp3",0, 0, 0);
   FSOUND_PlaySound (0,handle);

   // wait until the users hits a key to end the app
   while (!_kbhit())
   {
   }

   // clean up
   FSOUND_Sample_Free (handle);
   FSOUND_Close();
}

As a side note, I'd suggest you using C++ over C.