Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play MP3 files in C?

Tags:

c

mp3

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.

like image 563
Sam Avatar asked Jan 09 '09 17:01

Sam


People also ask

How do I play an MP3 file?

All you have to do is double-click on the MP3 file you want to listen to and by default, your audio player will open the file and start playing. If, however, you prefer a different audio player than either of those, changing the association of a file is a simple process on either Windows or macOS.


1 Answers

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.

like image 127
friol Avatar answered Oct 17 '22 13:10

friol