2

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

skaffman
  • 398,947
  • 96
  • 818
  • 769
Timothy
  • 608
  • 3
  • 10
  • 24
  • Oh yeah I'm using JLayer for playing the mp3 if that matters somehow – Timothy Jun 29 '10 at 13:23
  • possible duplicate of [How do I get a mp3 file's total time in Java?](http://stackoverflow.com/questions/3046669/how-do-i-get-a-mp3-files-total-time-in-java) – Daniel Cukier Dec 18 '14 at 14:46
  • Try using `jave-1.0.1.jar` library: [How do I get a mp3 file's total time in Java?](https://stackoverflow.com/questions/3046669) – davidu Apr 23 '18 at 06:22

3 Answers3

3

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

  • This method is not working for this track: https://www.dropbox.com/s/735fdkp9roskapw/39007.mp3?dl=0 - The track has 3:52, but this code sais it has 22:17. – Daniel Cukier Dec 18 '14 at 14:47
  • 1
    Currently the hashmap returned by .properties() doesn't even have a key called "duration", at least not that I can find. – php_coder_3809625 May 03 '16 at 13:55
  • @php_coder_3809625 - right. Here's the keys of the properties map for an MP3 I just tried it on. No "duration", nor is there anything that you could reasonably calculate one from: `mp3.copyright, date, mp3.framesize.bytes, mp3.vbr, mp3.framerate.fps, mp3.id3tag.track, title, mp3.channels, mp3.id3tag.genre, mp3.bitrate.nominal.bps, mp3.padding, mp3.version.mpeg, mp3.crc, mp3.original, mp3.frequency.hz, album, author, mp3.id3tag.v2, mp3.vbr.scale, mp3.version.encoding, mp3.version.layer, mp3.id3tag.v2.version, mp3.header.pos, comment` – Jules Oct 30 '17 at 06:11
1

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.

Simon
  • 706
  • 5
  • 11
  • This method is not working for this track: https://www.dropbox.com/s/735fdkp9roskapw/39007.mp3?dl=0 - The track has 3:52, but this code sais it has 22:17. – Daniel Cukier Dec 18 '14 at 14:47
0

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.

Sjoerd
  • 74,049
  • 16
  • 131
  • 175