基本上我有以下情况:
inputByteBuffer (capacity in debug) => 1024
byte[] inByteBufferArray = inputByteBuffer.array();
inByteBufferArray.length => 1031 ????
ByteBuffer array()方法说它"返回该缓冲区所基于的字节数组(如果有的话)。" ref
这在Android API 18中有效,做了些什么改变?有一个ByterBuffer arrayOffset()方法,我需要吗?
这是在Nexus 6设备上发生的,但我认为这不重要。
由于
private static int bufferSizeStatic = 1024;
private float[] Read(){
mAudioRecord.read(inputByteBuffer, bufferSize);
return readByteArray(inputByteBuffer.array(), inputByteBuffer.arrayOffset());
}
private float[] readByteArray(byte[] byteArray, int offSet) {
if (floatArray == null || floatArray.length!=(byteArray.length-offSet) / (numChannels*numBytePerFrame)){
floatArray = new float[(byteArray.length-offSet) / (numChannels*numBytePerFrame)];
}
if (numChannels == 1){
for (int i = 0; i < floatArray.length; i++){
short tempShort = (short) ((byteArray[2*i+1+offSet]<<8) + byteArray[2*i+offSet]);
floatArray[i] = (float) (tempShort / Math.pow(2,15));
}
} //TODO add stereo support
return floatArray;
}
答案 0 :(得分:0)
我找到了android代码:
MemoryRef(int capacity) {
VMRuntime runtime = VMRuntime.getRuntime();
buffer = (byte[])runtime.newNonMovableArray(byte.class, capacity + 7);
allocatedAddress = runtime.addressOf(buffer);
// Offset is set to handle the alignment: http://b/16449607
offset = (int)(((allocatedAddress + 7) & ~(long)7) - allocatedAddress);
isAccessible = true;
}
答案 1 :(得分:-1)
我不明白你在乎的原因。你的代码当然不应该关心。它的唯一原因是因为您滥用了API。您不应该从缓冲区中获取数据,而不是limit(),
而不是array().length()
或capacity()
。< / p>
您需要做的就是调整调用顺序并直接使用ByteBuffer
:
private static int bufferSizeStatic = 1024;
private float[] Read(){
// I don't know why you need to pass `bufferSize` here: `inputByteBuffer` already knows its own limit
mAudioRecord.read(inputByteBuffer, bufferSize);
return readByteArray(inputByteBuffer);
}
private float[] readByteArray(ByteBuffer byteBuffer) {
if (floatArray == null || floatArray.length!=(byteArray.limit()-byteArray.position()) / (numChannels*numBytePerFrame)){
floatArray = new float[(byteArray.limit()-byteArray.position()) / (numChannels*numBytePerFrame)];
}
byteBuffer.flip();
if (numChannels == 1){
for (int i = 0; i < floatArray.length; i++){
short tempShort = (short) ((ByteBuffer.getShort(i*2));
floatArray[i] = (float) (tempShort / 32768.0);
}
} //TODO add stereo support
byteBuffer.compact();
return floatArray;
}
E&安培; OE
你会发现这至少同样有效。