I need to get the values from the AudioInputStream and store it in a byte[]. Calling a read(byte[]) returns -1. If I'm able to read the values as integers, I can convert it to byte[]. How do I get around either of the problems ?
-
If read(byte[]) returns -1, it means that there's nothing more to read. Show the code you're using, you're probably constructing the inputsream wrong. – Kayaman Feb 28 '14 at 13:16
-
1Can you tell us the format of the audio file? Maybe it is not be an accepted format (e.g., instead of 16-bit, it might be 24- or 32-bit encoding). The best example code for reading a file in the Java Tutorials is in the following section. http://docs.oracle.com/javase/tutorial/sound/converters.html in the section "reading sound files" – Phil Freihofner Mar 01 '14 at 04:38
-
AudioFormat format = new AudioFormat(44100, 16,1,true,false); I capture the audio using mic and want to read the values into a byte[] – Srini Vas Mar 01 '14 at 07:22
-
1Possible duplicate of [Java How to store the audio data in a byte array.](https://stackoverflow.com/questions/13169044/java-how-to-store-the-audio-data-in-a-byte-array) – Joseph Quinsey Jun 07 '17 at 16:32
3 Answers
If the intention is to store the sound data in a byte[]
, the best approach is not to get an AudioInputStream
at all. Instead, just use a plain InputStream
.
The AudioInputStream
typically strips the first bytes from an input stream (because they contain formatting data) and then only provides the frames or samples of that stream. Doing it using an InputStream
on the other hand, you should be able to get all the bytes. Then once the entire data is in a byte array, an AudioInputStream
can be formed from the byte array (if needed).

- 168,117
- 40
- 217
- 433
UPDATED
Use apache commons IOUtils to read the bytes from the wav file.
InputStream is = -> your FileInputStream
ByteArrayOutputStream os = new ByteArrayOutputStream();
IOUtils.copy(is, os);
os.flush();
os.close();
byte[] ba = os.toByteArray();
You cannot read it directly to a ByteArrayOutputStream or you´re gonna get
java.io.IOException: stream length not specified
at com.sun.media.sound.WaveFileWriter.write(Unknown Source)
at javax.sound.sampled.AudioSystem.write(Unknown Source)

- 6,480
- 4
- 37
- 52
-
Call to IOUtils.copy() never returns. I have used the syntax you have specified. Commons version - 2.4. – Srini Vas Feb 28 '14 at 18:06
-
-
I have used the code from [link](http://www.codejava.net/coding/capture-and-record-sound-into-wav-file-with-java-sound-api). I used the code in the above answer to read the AudioInputStream. PS: I moved the declaration of ais (AudioInputStream reference) from start() to make ais a field of class. So it is visible in finish(). Modified sampleRate to 44100.0, sampleSizeInBits to 16 and channels to 1. – Srini Vas Mar 01 '14 at 07:22
-
you can capture to a file and then read the bytes from it. You cannot capture directly to a byte[] because the java audio API does not allow you to do that (see http://stackoverflow.com/questions/198679/convert-audio-stream-to-wav-byte-array-in-java-without-temp-file) – Leo Mar 01 '14 at 11:12
It is hard to understand what exactly you want: byte array with data or byte array with WAV file inside. Byte array with only sound data is a very simple task: copy from AudioInputStream to ByteArrayOutputStream.
The second case is more complex. In this case you should read from AudioInputStream to ByteArrayOutputStream and then form WAVE header. I can provide you two examples. I hope they will help you.
How to record audio to byte array - it is example of recording audio to byte array with wave file inside.
How to detect sound - it shows how AudioInputStream can be processed.

- 304
- 2
- 6