【发布时间】:2017-12-20 12:22:03
【问题描述】:
我的任务是将 short[] 数组转换为 byte[] 数组,因为需要通过套接字发送字节。这是 AudioTrack (Android) 的字节 对于转换使用这个post,特别是this 和this 当尝试将short转换为字节数组时,此方法只会产生白噪声:
val sampleBuffer = decoder.decodeFrame(frameHeader, bitstream) as SampleBuffer
val pcm = sampleBuffer.buffer //pcm is short[] array
byteBuf = ByteBuffer.allocate(pcm.size * 2) // because 1 short = 2 bytes
while (pcm.size > i) {
byteBuf.putShort(pcm[i])
i++
}
auddioTrack.write(byteBuf.array(), 0, byteBuf.limit());
但是这个转换工作正常:
var i = 0
val byteBuf = ByteBuffer.allocate(pcm.size * 2)
val buff = ByteBuffer.allocate(2)
//pcm size equals 2304
while (pcm.size > i) {
// byteBuf.putShort(pcm[i])
byteBuf.put(byteArrayOf((pcm[i].toInt() and 0x00FF).toByte(), ((pcm[i].toInt() and 0xFF00) shr (8)).toByte()))
i++
}
auddioTrack.write(byteBuf.array(), 0, byteBuf.limit());
为什么会这样?
【问题讨论】:
标签: java android kotlin bytebuffer short