我发现将短数组转换为字节数组,将字节数组转换为短数组,但不是将短数组转换为字节数组。
这是导致转换的代码
while(!stopped)
{
Log.i("Map", "Writing new data to buffer");
short[] buffer = buffers[ix++ % buffers.length];
N = recorder.read(buffer,0,buffer.length);
track.write(buffer, 0, buffer.length);
byte[] bytes2 = new byte[N];
我努力了
int i = 0;
ByteBuffer byteBuf = ByteBuffer.allocate(N);
while (buffer.length >= i) {
byteBuf.putShort(buffer[i]);
i++;
}
bytes2 = byteBuf.array();
和
ByteBuffer.wrap(bytes2).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(buffer);
但是,我在两者上都收到此错误(错误如果不完全相同但两者非常相似):
05-29 13:41:12.021:W/AudioTrack(9758):获取缓冲区()轨道 0x30efa0 禁用,重新启动
05-29 13:41:12.857: W/AudioWorker(9758): 读取语音 AudioWorker 时出错
05-29 13:41:12.857: W/AudioWorker(9758): java.nio.BufferOverflowException
05-29 13:41:12.857: W/AudioWorker(9758): 在 java.nio.ShortBuffer.put(ShortBuffer.java:422)
05-29 13:41:12.857: W/AudioWorker(9758): 在 java.nio.ShortToByteBufferAdapter.put(ShortToByteBufferAdapter.java:210)
05-29 13:41:12.857: W/AudioWorker(9758): 在 java.nio.ShortBuffer.put(ShortBuffer.java:391)
05-29 13:41:12.857: W/AudioWorker(9758): at com.avispl.nicu.audio.AudioWorker.run(AudioWorker.java:126)
在这里提供尽可能多的信息是使用字节数组之后的代码
Log.i("Map", "test");
//convert to ulaw
read(bytes2, 0, N);
//send to server
os.write(bytes2,0,bytes2.length);
System.out.println("bytesRead "+buffer.length);
System.out.println("data "+Arrays.toString(buffer));
}