This project has moved and is read-only. For the latest updates, please go here.

NLayer code for MP3 length

Jan 25, 2012 at 4:16 PM

I have a project that needs mp3 length calculation. I used NAudio, was working perfectly - until I published my code on Win2008 Server - the missing codec exception came up there and it wasn't possible to install it.

Then I switched to NLayer - downloaded the code, compiled it and used the functions of the BitStream class to go through the frames and calculate the full length by summarizing the frame-lengths (the same logic used with NAudio). The problem is that I got basically double-size...

I'm using the code below, please take a look and if you see anything wrong tell me what is it - I couldn't actually figure out from the NLayer code and I have the feeling that I'm very close already for switching again to something new. Might be something with the readFrame() call (readNextFrame underneath) - when debugging it always looked as returning the same thing plus if I used it as while condition it was a plain infinite cycle.

Bitstream bitStream = new Bitstream(inputStream);
double length = 0;
Header header = bitStream.readFrame();
while (bitStream.read_frame_data(header.framesize) > 0)
    length += (header.framesize * 8) / (header.bitrate() / 1000);
    header = bitStream.readFrame();
return length;

Thanks a lot!