Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java array()返回比ByteBuffer更大的数组_Java_Android_Arrays_Bytebuffer - Fatal编程技术网

Java array()返回比ByteBuffer更大的数组

Java array()返回比ByteBuffer更大的数组,java,android,arrays,bytebuffer,Java,Android,Arrays,Bytebuffer,基本上,我有以下场景: inputByteBuffer (capacity in debug) => 1024 byte[] inByteBufferArray = inputByteBuffer.array(); inByteBufferArray.length => 1031 ???? ByteBuffer array()方法表示它“返回此缓冲区所基于的字节数组(如果有)。” 这是在Android API 18中工作的,有什么变化吗?有一个ByterBuffer arrayOf

基本上,我有以下场景:

inputByteBuffer (capacity in debug) => 1024
byte[] inByteBufferArray = inputByteBuffer.array();
inByteBufferArray.length => 1031 ????
ByteBuffer array()方法表示它“返回此缓冲区所基于的字节数组(如果有)。”

这是在Android API 18中工作的,有什么变化吗?有一个ByterBuffer arrayOffset()方法,我需要它吗

这种情况发生在Nexus6设备上,但我认为这不重要

谢谢

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;
    }   
private static int bufferSizeStatic=1024;
私有浮点[]读(){
读取(inputByteBuffer,bufferSize);
返回readByteArray(inputByteBuffer.array(),inputByteBuffer.arrayOffset());
}
专用浮点[]readByteArray(字节[]byteArray,int偏移量){
if(floatArray==null | | floatArray.length!=(byteArray.length offSet)/(numChannels*numBytePerFrame)){
floatArray=新浮点[(byteArray.length offSet)/(numChannels*numBytePerFrame)];
}
如果(numChannels==1){
for(int i=0;ishort tempShort=(short)((byteArray[2*i+1+offSet])我不明白你为什么会在意。你的代码当然不应该在意。它在意的唯一原因是你误用了API。你不应该从缓冲区中取出不在缓冲区中的数据,这是由
limit(),
而不是
array().length()
,或者
capacity()给出的

您只需调整调用顺序并直接使用
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;
    }   
private static int bufferSizeStatic=1024;
私有浮点[]读(){
//我不知道为什么需要在这里传递'bufferSize','inputByteBuffer'已经知道了它自己的限制
读取(inputByteBuffer,bufferSize);
返回readByteArray(inputByteBuffer);
}
专用浮点[]读字节数组(ByteBuffer ByteBuffer){
if(floatArray==null | | floatArray.length!=(byteArray.limit()-byteArray.position())/(numChannels*numBytePerFrame)){
floatArray=new float[(byteArray.limit()-byteArray.position())/(numChannels*numBytePerFrame)];
}
byteBuffer.flip();
如果(numChannels==1){
for(int i=0;i
E&OE

你会发现这至少同样有效。

我发现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;
}
资料来源:
ByteBuffer
没有
length
字段。什么是
inputByteBuffer.length
ByteBuffer.array()
不返回
字节[]。
真正的代码是什么?抱歉,它是字节[]而不是字节[].和1024是调试时提取的inputByteBuffer容量。您可能应该在问题中澄清这一点。我在添加偏移量之前发布了这个问题,偏移量没有帮助,它是4,偏移量=4,容量=1024,数组长度=1031…关键是您的代码不应该在意。您的原始代码无效。您的代码崩溃,
java.lang.IndexOutOfBoundsException:index=0,limit=0,type=2的大小行它不会崩溃,但我得到的只是噪音,即使我向后索引缓冲区…这就是为什么我喜欢使用ArrayAnyWay我通过索引数组中的字节来获得它,好吧,所以ByteBuffer中没有任何内容,或者你以一种奇怪的方式将东西放在那里,也许是通过访问底层数组,而不是使用
putShort()的ead。