3

I want to convert a byte array into an AudioInputStream. The byte array was filled from a *.wav file before. I have the following code:

    public static AudioInputStream writeBytesBackToStream(byte[] bytes) {
    ByteArrayInputStream baiut = new ByteArrayInputStream(bytes);
    AudioInputStream stream = null;
    try {
        stream = AudioSystem.getAudioInputStream(baiut);
    } catch (UnsupportedAudioFileException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if(stream.equals(null) || stream == null) {
        System.out.println("WARNING: Stream read by byte array is null!");
    }
    return stream;
}

Now I only want to convert this byte array into an AudioInputStream, but an UnsupportedAudioFileException is thrown:

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

Has anyone got an idea?

3
  • Use this to confirm that it supports wav file, and also test passing the wav FIle to the getAudioInputStream... so you can check if the problem is with the bytearray or the file itseld Commented Jun 12, 2013 at 8:08
  • Can you post the wav file loading code to check that your byte array is correct? Commented Jun 12, 2013 at 8:15
  • I believe Durandel's answer is correct, but reading this primer may help: blog.bjornroche.com/2013/05/… Commented Jun 12, 2013 at 13:18

2 Answers 2

8

Another way to do this, if the data is actually PCM, is to set the AudioFormat parameters manually. Here is an example to create an A minor.

    byte[] b = new byte[64000];
    //lets make a 440hz tone for 1s at 32kbps, and 523.25hz.
    for(int i = 0; i<b.length/2; i++){

        b[i*2+1] = (byte)(127*Math.sin(4*Math.PI*440.0/b.length*i));
        b[i*2]  = (byte)(127*Math.sin(4*Math.PI*523.25/b.length*i));
    }

    AudioInputStream stream = new AudioInputStream(
        new ByteArrayInputStream(b), 
        new AudioFormat(16000, 8, 2, true, false), 
        64000
    );

Also be careful if you're making bytes into sounds and you have earphones in.

Sign up to request clarification or add additional context in comments.

Comments

1

If you properly read the WAV file, the byte array will just contain raw PCM data. Consequently the AudioSystem can not identify the format of the stream and throws the exception.

This can not work by design. You need to provide a complete audio format image in your stream to have AudioSystem recognize what format the stream is, not just raw data.

2 Comments

Thank you, writing a .wav header is the solution. I thought getting an AudioInputStream is everytime possible, even without a header.
@Kosch If you think a little about it, that would be impossible to implement. How would getAudioInputStream() determine if its mono/stereo, bytes per sample, byte order and samplerate from just raw data?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.