0

After sending a REST request the server brings back a really long string like this:

RIFF\nt WAVEfmt ...(a lot of extra characters)

This is supposed to be a .wav file. From there I get a byte array using Java:

byte[] audio = restResponse.getResponseBody().getBytes();

Which returns something like this:

[82, 73, 70, 70, 10, 116, 32, 0, 87, 65, 86, 69, 102, 109, 116, 32, 18...

After that I write that array into a file:

FileUtils.writeByteArrayToFile(new File("C:/Users/user/Desktop/temp.wav"), audio);

The problem is, I've tried 2-3 audioplayer + importing the raw data to Audacity (tried different frequencies, channels...) but all I hear is static.

ltd9938
  • 1,444
  • 1
  • 15
  • 29
Mikel SS
  • 13
  • 2
  • The original bytes have been encoded somehow to be represented as a string, you have to know how it was encoded to properly decode it (e.g Base 64 ). – Arnaud Jan 03 '19 at 15:05
  • Also, be sure to strip off the "RIFF\nt WAVEfmt " from in front of the actual bytes. – Steve11235 Jan 03 '19 at 15:08
  • Please share the code you use to send request. Without code sample nobody can help you. @Steve11235 Bad idea. This just corrupt the data, nothing more. – user882813 Jan 03 '19 at 15:12
  • In C, a string is used to contain a sequence of bytes. In Java, a String is not suitable for holding bytes; converting bytes to and from Strings will *corrupt* the bytes. Do not use String at all. Always use byte arrays to hold bytes. – VGR Jan 03 '19 at 16:22
  • If you look at the byte sequence in the post, it starts with the codes for the characters R I F F \n, etc. I suspect that the actual WAV content is following that header. – Steve11235 Jan 03 '19 at 20:38

1 Answers1

0

As VGR said (thanks a lot!), the problem was the way the Rest client was converting bytes into a String.

By using Apache's HttpClient I was able to get the response bytes correctly.

byte[] response = method.getResponseBody();

FileUtils.writeByteArrayToFile(new File("C:/Users/user/Desktop/temp.wav"), response);

Mikel SS
  • 13
  • 2