Timothy
Timothy

Reputation: 618

Read out Time/Length/Duration of an Mp3 song in Java

I wonder how I can read out the duration of an mp3 song. If I'm right it's not an ID3 Tag so I guess I have to calculate it somehow ? For the rest of the ID3 Tags I'm using this libary: http://javamusictag.sourceforge.net/index.html

Cheers

Upvotes: 2

Views: 17059

Answers (3)

Mardo Del Cid
Mardo Del Cid

Reputation: 54

I would change the code to...

File file = new File("filename.mp3");
AudioFileFormat baseFileFormat = new MpegAudioFileReader().getAudioFileFormat(file);
Map properties = baseFileFormat.properties();
Long duration = (Long) properties.get("duration");

using MP3SPI to use new MpegAudioFileReader() instead of AudioSystem, because i was getting an UnsupportedAudioFileException occured : file is not a supported file type with mp3 files

Upvotes: 3

Simon
Simon

Reputation: 706

You could try using MP3SPI which is based on JLayer. The following code will then allow you to get the song duration:

File file = new File("filename.mp3");
AudioFileFormat baseFileFormat = AudioSystem.getAudioFileFormat(file);
Map properties = baseFileFormat.properties();
Long duration = (Long) properties.get("duration");

You can also use MP3SPI to get at the ID3 tags which might save you an extra dependency.

Upvotes: 1

Sjoerd
Sjoerd

Reputation: 75568

Use some MP3 library. The MP3 format is not that easy that there is some simple way to read out the length of a song.

Upvotes: 0

Related Questions