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 1000000. But this is resulting in empty values at the end of the array. So, I wanted to create a dynamically increasing array and I found that ArrayList is the solution. But the read() function of the AudioInputStream class reads bytes only into a byte array! How do I pass the values into an ArrayList instead?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
You can have an array of byte like:
To convert it back to arrays
(Then, you will have to write a converter to transform
Byte[]
tobyte[]
).EDIT: You are using
List<Byte>
wrong, I'll just show you how to readAudioInputStream
simply withByteArrayOutputStream
.PS An
IOException
is thrown ifframeSize
is not equal to1
. Hence use a byte buffer to read data, like so:ArrayList
isn't the solution, ByteArrayOutputStream is the solution. Create aByteArrayOutputStream
write your bytes to it, and then invoketoByteArray()
to get the bytes.Example of what your code should look like:
Something like this should do:
Sorry if the code is a bit wrong. I haven't done Java for a while.
Also, be careful if you do implement it as a while(true) loop :)
Edit: And here's an alternative way of doing it that reads more bytes each time:
Note that both read() methods throw an IOException - handling this is left as an exercise for the reader!