我从MediaCodec接收ByteBuffer对象,目的是将ByteBuffer的内容复制到我的ByteBuffer对象,从而缩小容量。像这样:
// get the ByteBuffer from MediaCodec
// the capacity of encodedBufferFromMediaCodec is 12345678
encodedBufferFromMediaCodec = getByteBuffer();
// copy the content of encodedBufferFromMediaCodec to my ByteBuffer
// the capacity of myBuffer is 123456
// the content'size of encodedBufferFromMediaCodec is 123
myBuffer.put(encodedBufferFromMediaCodec);
以上是我的目标。但我得到以下例外:
10-30 14:48:54.621 E/AndroidRuntime( 2999): Process: com.jerikc.demo, PID: 2999
10-30 14:48:54.621 E/AndroidRuntime( 2999): java.nio.BufferOverflowException
10-30 14:48:54.621 E/AndroidRuntime( 2999): at java.nio.ByteBuffer.put(ByteBuffer.java:753)
那怎么办呢?
答案 0 :(得分:1)
答案 1 :(得分:1)
正如EJP建议的那样,这应该涵盖大多数情况。如果encodedBufferFromMediaCodec
有超过12345个字节,它将无法工作。在这种情况下,您将获得BufferOverflowException。
ByteBuffer myBuffer = ByteBuffer.allocate(12345);
ByteBuffer encodedBufferFromMediaCodec = getByteBuffer();
encodedBufferFromMediaCodec.flip();
myBuffer.put(encodedBufferFromMediaCodec);
encodedBufferFromMediaCodec.compact();
答案 2 :(得分:0)
ByteBuffer myBuffer = ByteBuffer.allocate(123456);
encodedBufferFromMediaCodec.rewind();
int i = 0;
while (i < 123456) {
myBuffer.put(encodedBufferFromMediaCodec.get());
i++;
}
myBuffer.flip();
请注意,如果encodedBuffer包含123456+的数据,您将丢失该数据。