Search code examples
javaaudiomp3

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


Solution

  • 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