1

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?

Mena
  • 47,782
  • 11
  • 87
  • 106
Skylion
  • 2,696
  • 26
  • 50
  • possible duplicate of [Java Exception Reading Stream from Resource .wav](http://stackoverflow.com/questions/9659842/java-exception-reading-stream-from-resource-wav) – Mena Aug 08 '13 at 17:34
  • I don't have access to the file in this case though, just the InputStream. – Skylion Aug 08 '13 at 17:36
  • 1
    Java Sound does not support playing MP3 out of the box. You need a plugin for that. See [this post](http://stackoverflow.com/questions/8392241/where-can-i-download-the-mp3plugin-jar-to-play-mp3s-in-java-code). The accepted answer has a link to download mp3 plugin. Get the jar and add it to your classpath – c.s. Aug 08 '13 at 17:52

1 Answers1

0

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

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • How can extract the RAW data from the InputStream then? – Skylion Aug 08 '13 at 17:38
  • Depends what is it. Is it MP3 or what ? Generally you have to decode it. RAW data-decoded and uncompressed audio samples – Antoniossss Aug 08 '13 at 17:45
  • It's an InputStream containing MP3 data. – Skylion Aug 08 '13 at 18:05
  • Than get yourself some kind of mp3 decoding library, decode stream and play it than. Nothing to add more here i think, or just simply use jLayer. Here you havesample code of decoding MP3 frames using jLayer http://stackoverflow.com/questions/12099114/decoding-mp3-files-with-jlayer – Antoniossss Aug 08 '13 at 18:13