0
\$\begingroup\$

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))
 .array();
 player.playBlock(das);

or split like this:

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

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

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Feb 21, 2016 at 13:44
\$\endgroup\$
1
  • \$\begingroup\$ What kind of object is player? \$\endgroup\$ Commented Apr 9, 2016 at 4:00

1 Answer 1

1
\$\begingroup\$

Implementation

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));
}
answered Feb 21, 2016 at 14:19
\$\endgroup\$
3
  • \$\begingroup\$ Thank you, so I'd just call this method twice in my case? \$\endgroup\$ Commented Feb 21, 2016 at 14:31
  • \$\begingroup\$ Yes, you would. \$\endgroup\$ Commented 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\$ Commented Feb 21, 2016 at 18:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.