2017-12-20 17 views
0

내 작업은 소켓을 통해 바이트를 전송해야하기 때문에 short [] 배열을 byte [] 배열로 변환합니다. 이 시도는 바이트 배열로 짧은 변환 할 때이 방법은, 오직 백색 잡음을 준다 특히, thisthis 를 사용이 post을 변환 AudioTrack (안드로이드) 에 대한 바이트 :내 경우 ByteBuffer putShort (value) 메서드가 올바르게 작동하지 않는 이유는 무엇입니까?

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()); 

왜 발생 했습니까?

답변

0

byteBuf.array().size 많은 바이트가 버퍼에 기록되었는지 여부에 관계없이 버퍼 크기 (pcm.size * 2)를 반환합니다. 대신 byteBuf.limit()을 원할 수도 있습니다.

+0

시도하지만 도움이되지 않습니다. 내 메시지를 편집했습니다. – Sergey

+0

@Sergey'pcm.size * 2'의 값은 무엇입니까? 'byteBuf.limit()'의 값은 무엇입니까? 'pcm [i]'의 타입은 무엇입니까? – Gili

+0

타입은 short [] - 배열이므로, 배열 short를 배열 바이트로 변환 할 때, byte []의 크기는 * 2가된다. – Sergey