开发者

Extract duration from a U-LAW audio file with Java

开发者 https://www.devze.com 2022-12-12 14:49 出处:网络
I\'m trying to extract the duration from a audio file. This audio file is a U-LAW (G.711/MONO 8.000Hz).

I'm trying to extract the duration from a audio file.

This audio file is a U-LAW (G.711/MONO 8.000Hz).

static AudioFormat ULAW_FORMAT = new AudioFormat(AudioFormat.Enc开发者_如何转开发oding.ULAW, 8000, 8, 1, 1, 8000, false);

And all the time I'm receiving a exception:

javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file

Small part of my source, where I'm receiving the exception:

AudioInputStream inputFileStream = AudioSystem.getAudioInputStream(file);

or

AudioInputStream inputFileStream = AudioSystem.getAudioInputStream(ULAW_FORMAT, inputFileStream);

Questions

I'm doing something wrong?

Java is unable to read this type of audio file?

Source failing?

Thanks for any tips!

UPDATING with jitter comment

I tried with this dummy source:

public static void main(String[] args) {
    String selectFile = "\\audio\\543288";
    AudioInputStream source;
    try {
        source = AudioSystem.getAudioInputStream(new File(selectFile));
        AudioSystem.getAudioInputStream(AudioFormat.Encoding.ULAW, source);
    } catch (UnsupportedAudioFileException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

And is always failing in the first line in the try!


µ-law is a sample value encoding and not a file format, so using the AudioSystem to find a suitable AudioReader for a "µ-law file" (whatever that is) is going to fail. If I'm not mistaken, Sun's VM has built-in support for AIFF, AU and WAV files.

I'm now only guessing, but if your file contains raw µ-law data without any file format header, you can simply create an AudioInputStream directly, without going through the utility methods in AudioSystem:

int length = ... // file length in number of samples
FileInputStream fis = new FileInputStream("...");

AudioInputStream ais = new AudioInputStream(
    fis, new AudioFormat(AudioFormat.Encoding.ULAW, 8000, 8, 1, 1, 8000, false), length);

And at last: if your first question is really correct (you are only trying to get the duration of the file), there is no need to fight with the AudioInputStream. If the file really contains raw µ-law data with 8000 samples/s and each sample is one byte, it's eeh, well you get it?


Are you sure the audio file is in valid format? The exception normally means that java can't interpret the data read form the file as valid audio data.

Are you sure the settings you provide with ULAW_FORMAT are correct?

I'm not familiar with µ-law encoding but are the values for frameSize and frameRate correct?

You could also try

AudioSystem.getAudioInputStream(AudioFormat.Encoding.ULAW, inputFileStream);

If that too doesn't work either the file has invalid audio data or java has some parsing bug.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号