36

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.

Oak Bytes
  • 4,649
  • 4
  • 36
  • 53
Sam
  • 361
  • 1
  • 3
  • 3
  • What kind of license should this library have? I'd guess there are a couple of GPL ones (although the mp3 format/algorithm always has/had it's license issues) but these might not fit your project. Also: windows/osx/linux? – Simon Groenewolt Jan 09 '09 at 17:14

9 Answers9

26

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.

Gowtham
  • 11,853
  • 12
  • 43
  • 64
friol
  • 6,996
  • 4
  • 44
  • 81
5

The BASS DLL is really easy to use and will probably do what you need. It is only free for non-commercial use though.

If you need more control, you will need a codec (I prefer libMad) and some sound output API like DirectSound on Windows or ALSA or Linux (or whatever Linux guys use for sound this week)

Tamas Czinege
  • 118,853
  • 40
  • 150
  • 176
3

I don't know if it is "the easiest way", but you could have a look at SDL (along with SDL_sound).

Guishu
  • 41
  • 2
2

If you are on Windows or OSX, I recommend BASS (http://www.un4seen.com/bass.html)

You can download the library and look at code sample to get started. The "contest" example in C directory is a good start point.

Gant
  • 29,661
  • 6
  • 46
  • 65
2

On Win32, you don't need any library. Use standard Win32 api (mp3 is native)

See on Adv. Win32 api newsgroup : news://comp.os.ms-windows.programmer.win32 where it 's a FAQ.

1

If u can use C++ and if u are working on windows platform than use WMp3

That Library is easy to work with and let you play, pause, seek on mp3 files.

Lav
  • 1,850
  • 15
  • 17
0

Go here:

http://code4k.blogspot.com/2010/05/playing-mp3-in-c-using-plain-windows.html

This website has a zip in which you can view how this person generated the code for an mp3 player.

You can also check out: http://www.codeguru.com/cpp/g-m/directx/directshow/article.php/c19079/Simple-C-MP3-Player-Class.htm

or

http://www.ucancode.net/Visual_C_Control/Play-MP3-File-VC-Sample-Player.htm

0

alternatively something someone's already written that will just run and quit.

You can use mpg123 (or the fixed point port of it, mpg321)

mpg123 <mp3file>

will play an mp3 file and quit.

codelogic
  • 71,764
  • 9
  • 59
  • 54
0

mpg123 has a generic remote interface that you access by starting the executable with the -R option. You can then send commands (such as load, pause etc) over a fifo pipe or to stdin of the subprocess. If nothing else it's easy to debug and test manually.

Hank
  • 2,907
  • 3
  • 20
  • 15