This is a question about the file format of MP3s.
I've been hunting for a way to get an MP3 duration. Since I'm using JLayer SPI to decode the MP3 I've discovered that this is possible where the audio source is a file.
AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(source);
Long microseconds = (Long) fileFormat.properties().get("duration");
System.out.println(microseconds);
However when I take the similar code and the very same MP3 and change the source to a URL, JLayer does not provide the duration:
AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(source.toURI().toURL());
Long microseconds = (Long) fileFormat.properties().get("duration");
System.out.println(microseconds);
This is an important distinction because some of the music I want to play will be sourced from URLs not local files.
I suspect that this is due to limitations of the file format.
This leads me to a very simple question. Is it possible to know the duration of an MP3 before the entire file has been downloaded?
To compute the duration you would need to divide the length of the file by the bit rate.
I presume this method falls flat the moment you have variable bitrate mp3s (as most are these days). – Hognut