'Hot to get mp3 file duration with mp3spi?

i want to get the duration of the following mp3file with mp3spi:

http://www.thapps.net/recordings/troporocks.mp3

I got only the following properties

mp3.crc=false
mp3.copyright=false
mp3.padding=false
mp3.channels=2
mp3.version.mpeg=1
mp3.framerate.fps=38.28125
mp3.framesize.bytes=413
mp3.version.layer=3
mp3.frequency.hz=44100
mp3.header.pos=0
mp3.bitrate.nominal.bps=128000
mp3.vbr.scale=0
mp3.version.encoding=MPEG1L3
mp3.mode=0
mp3.vbr=false
mp3.original=true

When using this code:

ByteArrayInputStream bais = new ByteArrayInputStream(audioFile.file); 

AudioFileFormat baseFileFormat = new MpegAudioFileReader().getAudioFileFormat(bais);

Map<String, Object> properties = baseFileFormat.properties();
Integer duration = (Integer) properties.get("duration");

But the duration is not available in the properties map.

How can I get the duration?



Solution 1:[1]

I tried using File instead of ByteArrayInputStream and it works.

Consider to change your code in something like this:

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

Which version of mp3Spi are you using?

Solution 2:[2]

The problem is, that new MpegAudioFileReader().getAudioFileFormat(bais) points to the inherited method from TAudioFileReader

This is how I extended the MpegAudioFileReader to make it work:

Added this method around line 300

public AudioFileFormat getAudioFileFormat(InputStream inputStream) throws UnsupportedAudioFileException, IOException {
    return getAudioFileFormat(inputStream, inputStream.available());
}

Commented this line (around 400)

//FileInputStream fis = (FileInputStream) inputStream;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 pixel