I want to read bytes from a wave file into an array. Since the number of bytes read depends upon the size of the wave file, I\'m creating a byte array with a maximum size of
You can have an array of byte like:
List arrays = new ArrayList();
To convert it back to arrays
Byte[] soundBytes = arrays.toArray(new Byte[arrays.size()]);
(Then, you will have to write a converter to transform Byte[]
to byte[]
).
EDIT: You are using List
wrong, I'll just show you how to read AudioInputStream
simply with ByteArrayOutputStream
.
AudioInputStream ais = ....;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int read;
while((read = ais.read()) != -1) {
baos.write(read);
}
byte[] soundBytes = baos.toByteArray();
PS An IOException
is thrown if frameSize
is not equal to 1
. Hence use a byte buffer to read data, like so:
AudioInputStream ais = ....;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead = 0;
while((bytesRead = ais.read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
}
byte[] soundBytes = baos.toByteArray();