byte[] eachPass = new byte[1600]; //used to store data from TargetDataLine for each pass
byte[] backingArray = new byte[16000]; //the complete data for one second
ByteBuffer buffer = ByteBuffer.wrap(backingArray); //buffer which stores the complete data
short[] audioSample = new short[16000/2]; //audio samples to be encoded
int passCounter = 0; /* After 10th pass, convert the byte[] to short[]
* using ByteBuffer */
int seconds = 0; // used to store the position of the packet
while(keepCapturing == true){
-- set up the java.awt.Robot and TargetDataLine before entering the loop --
-- use java.awt.Robot to record the screen --
-- do some other stuff, if needed --
fromMic.read(eachPass,0,eachPass.length); // read data from microphone
buffer.put(eachPass); //put it in a bigger buffer
if(passCounter!=0 && passCounter%10==0){ // is it 10th frame?
passCounter = 0; //reset counter
seconds++;
buffer.asShortBuffer.get(audioSamples); //get short[] in BigEndian format
-- encode the audio at position (seconds-1) --
buffer.clear();
}else{
passCounter++;
}
buffer.position()
returns 16000 in the if
statement, I get a BufferUnderflowException
when I do buffer.asShortBuffer.get(audioSamples);
java.util.Arrays.toString()
to see what my eachPass
and audioSamples
contains, I got some numbers like -107, 0, 32, etc in eachPass and all zeroes in audioSamples. Why? You are forgetting to flip the buffer before reading the data, this is why nothing in being written into audioSamples
.
buffer.flip();
buffer.asShortBuffer.get(audioSamples);