如何将短数组转换为字节数组
我发现将短数组转换为字节数组,将字节数组转换为短数组,但不能将短数组转换为字节数组。
这是导致转换的代码
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): getBuffer() 轨道0x30efa0禁用,重新启动
05-29 13:41:12.857: W/AudioWorker(9758): 读取语音音频工作者时出错
05-29 13:41:12.857: W/AudioWorker(9758): java.nio.BufferOverflowException
05-29 13:41:12.857: W/AudioWorker(9758): at java.nio.ShortBuffer.put(ShortBuffer.java:422)
05-29 13:41:12.857: W/AudioWorker(9758): at java.nio.ShortToByteBufferAdapter.put(ShortToByteBufferAdapter.java:210)
05-29 13:41:12.857: W/AudioWorker(9758): at 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));
}