javafileaudiowavriff

Extract WAV file from Bytes Array


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.


Solution

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