0

I want to get duration in millisecond of a m4a file in java, how to do this ?

I tried code below

    public static long getAudioLength(File file) throws IOException, UnsupportedAudioFileException {
        AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(file);
        long frames = fileFormat.getFrameLength();
        float frameRate = fileFormat.getFormat().getFrameRate();
        return (long) (frames / frameRate * 1000);
    }

It throws:

javax.sound.sampled.UnsupportedAudioFileException: File of unsupported format
    at java.desktop/javax.sound.sampled.AudioSystem.getAudioFileFormat(AudioSystem.java:980) ~[na:na]
    at getAudioLength(AudioUtils.java:17) ~[classes/:na]

I am using JDK 11


Result of javax.sound.sampled.AudioSystem#getAudioFileReaders(), seems JDK lacks of a suitable default reader.

[com.sun.media.sound.WaveExtensibleFileReader@75cf5965,
 com.sun.media.sound.WaveFloatFileReader@309ae93,
 com.sun.media.sound.WaveFileReader@727c801f,
 com.sun.media.sound.SoftMidiAudioFileReader@84d1378,
 com.sun.media.sound.AuFileReader@7c67765,
 com.sun.media.sound.AiffFileReader@49753c38]
djy
  • 737
  • 6
  • 14

0 Answers0