将 ByteArray 转换为 ShortArray

Convert ByteArray to ShortArray

我在 Kotlin/Java 中有音频数据的字节数组。

当我使用下面的代码时,创建了 ShortBuffer,但里面的 short[] 是空的。

    var short1 = ByteBuffer.wrap(samples.data).order(ByteOrder.BIG_ENDIAN).asShortBuffer()

当我调用函数时 .array() 无法转换,因为缓冲区中的 short[] 为 null。

我用 arrays/buffers 操作的方式不对?

示例数据是 byte[],其中包含 AndroidWebRtc

中的音频数据

ShortBufferarray() 方法是一个可选操作,并且 仅在 ShortBuffer 由短数组 ( short[]).

public final short[] array()

Returns the short array that backs this buffer (optional operation).

Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa.

Invoke the hasArray method before invoking this method in order to ensure that this buffer has an accessible backing array.

由于您的 ShortBuffer 没有 支持 short[],因为它是由 ByteBuffer 创建的,array()方法将抛出 UnsupportedOperationException.

您可以使用 hasArray() 方法来检查调用 array().

是否安全