我发现将一个短数组转换为字节数组,将字节数组转换为短数组,但不是短数组转换为字节数组.
以下是导致转换的代码
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];
Run Code Online (Sandbox Code Playgroud)
我试过了
int i = 0;
ByteBuffer byteBuf = ByteBuffer.allocate(N);
while (buffer.length >= i) {
byteBuf.putShort(buffer[i]);
i++;
}
bytes2 = byteBuf.array();
Run Code Online (Sandbox Code Playgroud)
和
ByteBuffer.wrap(bytes2).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(buffer);
Run Code Online (Sandbox Code Playgroud)
但是我在两者上都收到了这个错误(错误如果不完全相同但两者非常相似):
05-29 13:41:12.021:W/AudioTrack(9758):gainBuffer()轨道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):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, …Run Code Online (Sandbox Code Playgroud) 我正在尝试将短路转换为2个字节...然后从这2个字节尝试获得相同的短路值.为此,我写了这段代码:
short oldshort = 700;
byte 333= (byte) (oldshort);
byte byte2= (byte) ((oldshort >> 8) & 0xff);
short newshort = (short) ((byte2 << 8) + byte1);
System.out.println(oldshort);
System.out.println(newshort);
Run Code Online (Sandbox Code Playgroud)
对于700(oldshort)的值,newhosrt是444.经过一些测试后,它看起来像这样的代码只适用于某些值.就像...如果oldshort = 50,那么它将正常工作..但如果它是-200,或更大的值超过127(我认为)它不起作用.我想有签名字节,二进制补码等问题...但我无法弄清楚如何解决它.
任何的想法??在java中以任何本地方式执行此操作?提前致谢!