Search code examples
javaexceptionaudiomp3inputstream

AudioSystem.getInputStream(InputStream) Exception


I am trying to convert an InputStream containing data from an MP3 file to an AudioInputStream using the Java Sound API.

However, I keep encountering an Exception when I try to do so.

Here is the method that causes the issue:

public static int[][] getAmpFromInputStream(InputStream is) throws Exception{
    BufferedInputStream bs = new BufferedInputStream(is);
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(bs);
    byte[] bytes = new byte[(int) (audioInputStream.getFrameLength()) * (audioInputStream.getFormat().getFrameSize())];
    audioInputStream.read(bytes);

    // Get amplitude values for each audio channel in an array.
    return getUnscaledAmplitude(bytes, 1);
}

The Exception that is thrown is:

javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream
    at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
    at PitchChanger.getAmpFromInputStream(PitchChanger.java:27)
    at GUI$PlayMP3Thread.silly(GUI.java:128)
    at GUI$PlayMP3Thread.run(GUI.java:115)
    at java.lang.Thread.run(Unknown Source)

This confuses me however as I use the same InputStream to play into Javazoom.jl.player.Player, and it works fine there.

So why can't the InputStream be converted to an AudioInputStream?

Doesn't Java Sound support the MP3 file format?


Solution

  • Javazoom supports MP3 (has decoder) when AudioSystem must have RAW data provided (raw samples as in WAV format).