3

I want to use JLayer to decode an MP3 file. I have searched and searched for documentation and examples on how exactly to do this, and have turned up nothing of use. Everything I find is embedded in other examples or references JavaSound, which is unacceptable in my case.

I feel like this is incredibly easy, but I can't figure out how to do it. I don't know what the parameters are for

Decoder decoder = new Decoder();
decoder.decodeFrame(Header header, Bitstream stream);

or how to obtain them.

tl;dr How do I decode an MP3 file with nothing but JLayer? No MP3 SPI, JavaSound, Tritonus--nothing.

Albatross
  • 71
  • 1
  • 6
  • You want to decode and do what ? store as Wave file ? play it using speakers ? – m_vitaly Aug 23 '12 at 20:07
  • @VitalyPolonetsky Store the samples in an array. I just want to be able to get all the samples out of an mp3 file. – Albatross Aug 24 '12 at 16:49
  • So you want to store the samples for some future use (I suppose). But in what format you need the samples ? – m_vitaly Aug 27 '12 at 20:42
  • PCM. Jlayer has the decodeFrame method that returns a buffer of samples. I just want to know how to use that method to get a given amount of samples. – Albatross Sep 04 '12 at 21:24
  • @Albatross did u have the code for converting .mp3 to pcm data using jLayer ? – Udit Kumawat Jun 16 '15 at 11:31

1 Answers1

4

Figured it out myself.

Bitstream bitStream = new Bitstream(new FileInputStream("path/to/audio.mp3"));

while(condition){
    Decoder decoder = new Decoder();
    int[] samples = decoder.decodeFrame(bitStream.readFrame(), bitStream); //returns the next 2304 samples
    bitStream.closeFrame();

    //do whatever with your samples
}
Albatross
  • 71
  • 1
  • 6
  • Whatever you want. It controls how much of the mp3 is decoded. So if you wanted to decode the whole file, your condition would be whether there are more samples to decode or not. – Albatross Sep 26 '12 at 19:03
  • @Albatross: this is really interesting. Do you think this could be used to stream mp3 over internet? And also how do you play those samples after your while ? – dynamic Feb 10 '14 at 14:56