How to know the duration / total playing time of an mp3 file using java?

I am developing a Java application. Part of it involves playing an mp3 file through another application. I need to find the duration (total playing time) of an mp3 file. How to do it?

+4
source share
2 answers

You can do this very easily using JAudioTagger:

 Tag tag; java.util.logging.Logger.getLogger("org.jaudiotagger").setLevel(Level.OFF); audioFile = AudioFileIO.read(new File(filePath)); System.out.println("Track length = " + audioFile.getAudioHeader().getTrackLength()); 

This will print the length of the file track in filePath. Logger line - remove a lot of (possibly) unwanted information / debugging entries from JAudioTagger. In addition, JAudioTagger supports retrieving all kinds of tag metadata from different types of audio files (MP3, MP4, WMA, FLAC, Ogg Vorbis), as from embedded files. You can even easily get information about MusicBrainz, but I have not tried this yet. For more information:

http://www.jthink.net/jaudiotagger/examples_read.jsp

Here you can get the jar files:

http://download.java.net/maven/2/org/jaudiotagger/

+6
source

For small .mp3 files you can use:

 AudioFile audioFile = AudioFileIO.read(MyFileChooser.fileName); int duration= audioFile.getAudioHeader().getTrackLength(); System.out.println(duration); 

This will produce a string containing the minute and second duration in it. e.g. 316 to 3:16 .

Please note: this method is not suitable for large files.

0
source

All Articles