I am trying to calculate the duration of .wav files using java sound api . Java sound api supports .wav files with PCM S16LE codec but it doesn't seem to support .wav file with IMA WAV ADPCM codec. Is there any library that can help me get the duration of all kinds of .wav files with any kind of codec or is there any way that I can calculate the duration of the .wav files without using any library?
So far i have tried so much :
private Long getDuration(File file)
{
AudioFileFormat fileFormat = null;
long duration = 0;
try
{
fileFormat = AudioSystem.getAudioFileFormat(file);
if(fileFormat.getType().toString().equalsIgnoreCase(StringConstants.MP3)) {
Map<?, ?> properties = fileFormat.properties();
Long microseconds = (Long) properties.get(StringConstants.DURATION);
//converting to milliseconds
duration = microseconds / 1000;
}
else if(fileFormat.getType().toString().equalsIgnoreCase(StringConstants.AUDIO_WAVE)) {
long frames = fileFormat.getFrameLength();
double durationInSeconds = (frames + 0.0) / fileFormat.getFormat().getFrameRate();
duration = Math.round(durationInSeconds * 1000);
}
}
catch (UnsupportedAudioFileException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return duration;
}
is there any way i can use the same logic that i have used here to calculate duration for .wav files of IMA WAV ADPCM AUDIO ? if yes, how do i get the frame length and frame rate of the file?