I want to play some audio of two byte arrays. I was wondering whether it would be better to play them as two separate arrays both calling the method, or to put them into one array and play it from there.

Note: The starting from position 4 because the first 4 bytes are saved for the sequence number in the array.

All in one array:

   byte[] das = ByteBuffer.allocate((temp.length-4)*2)
                        .put(Arrays.copyOfRange(temp, 4, temp.length - 4))
                        .put(Arrays.copyOfRange(udpPacketBytes, 4, udpPacketBytes.length - 4))

or split like this:

  byte[] filteredByteArray = Arrays.copyOfRange(temp, 4, temp.length - 4);


                // play the current one
                byte[] fba = Arrays.copyOfRange(udpPacketBytes, 4, udpPacketBytes.length - 4);


I was also wondering whether there'd be a way to calculate the complexity of the two.

  • \$\begingroup\$ What kind of object is player? \$\endgroup\$ Apr 9, 2016 at 4:00

1 Answer 1



Better is a nebulous concept. The first one assumes that both arrays are the same length and fails to play all audio otherwise, so I'll say that's worse. I would suggest pulling the subarray computation into a method.

private void playAudio(final byte[] audio) {
    this.player.playBlock(Arrays.copyOfRange(audio, 4, audio.length - 4));
  • \$\begingroup\$ Thank you, so I'd just call this method twice in my case? \$\endgroup\$ Feb 21, 2016 at 14:31
  • \$\begingroup\$ Yes, you would. \$\endgroup\$
    – Eric Stein
    Feb 21, 2016 at 17:29
  • \$\begingroup\$ Had to move a few things around to get it to fit in my code, works great thanks \$\endgroup\$ Feb 21, 2016 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.