Search code examples
javajavasound

Unknown frame size


Trying to get the frame size of an audio file I am getting instead -1. I tried to look for the interpretation of of this result in the JavaDoc but it does not mention anything big. Here's the source code :

import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader;
/*....*/
File file = new File("/home/songs/audio.mp3");
MpegAudioFileReader mpegAudioFileReader = new MpegAudioFileReader();        
AudioInputStream audioInputStream = mpegAudioFileReader.getAudioInputStream(file); 
AudioFormat format = audioInputStream.getFormat();   
long frameSize = format.getFrameSize();//frameSize = -1
float frameRate = format.getFrameRate();//frameRate = 38.28125

Inspecting he format object gives this : MPEG1L3 44100.0 Hz, unknown bits per sample, stereo, unknown frame size, 38.28125 frames/second, I do not know why the frame size is unknown although it does appear on my audio file properties :

enter image description here

Any help is more than appreciated. Thanks.


Solution

  • getFormat() etc is implemented by the MPEG guys so it returns what they have - probably they left this blank or unable to extract;

    If you put another .wav file you will probably get 2:

    try {
      audioInputStream=AudioSystem.getAudioInputStream(new File(".......wav"));
    
      System.out.println(audioInputStream.getFormat().getFrameSize());
    
    } catch (Exception e) {
            e.printStackTrace();
    }
    

    Other notes: I dont see the Frame size in your display; it's rather the sample/bit rate so be sure to differentiate about that.

    But for mp3 you have to live with that.

    You can also create your own format if that helps - dont know your application

    AudioFormat format = audioInputStream.getFormat();
    newFormat=new AudioFormat(
              AudioFormat.Encoding.PCM_SIGNED,
              format.getSampleRate(),
              16,
              format.getChannels(),
              format.getChannels() * 2,
              format.getSampleRate(),
              false);