How do I get a mp3 file's total time in Java?

Posted by Tom Brito on Stack Overflow See other posts from Stack Overflow or by Tom Brito
Published on 2010-06-15T15:33:44Z Indexed on 2010/06/15 16:02 UTC
Read the original article Hit count: 423

Filed under:
|
|
|

The answers provided in How do I get a sound file’s total time in Java? work well for wav files, but not for mp3 files.

They are (given a file):

AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
AudioFormat format = audioInputStream.getFormat();
long frames = audioInputStream.getFrameLength();
double durationInSeconds = (frames+0.0) / format.getFrameRate();  

and:

AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
AudioFormat format = audioInputStream.getFormat();
long audioFileLength = file.length();
int frameSize = format.getFrameSize();
float frameRate = format.getFrameRate();
float durationInSeconds = (audioFileLength / (frameSize * frameRate));

They give the same correct result for wav files, but wrong and different results for mp3 files.

Any idea what do I have to do to get the mp3 file's duration?

© Stack Overflow or respective owner

Related posts about java

Related posts about file