I have been trying to send the recorded audio to the a controller method like shown below:
JS
$scope.convertBlobToArrayBuffer = function (audioBlob) {
var fileReader = new FileReader();
fileReader.onload = function() {
$scope.saveRecordingToServer( this.result );
};
fileReader.readAsArrayBuffer(audioBlob);
}
$scope.saveRecordingToServer = function (audioArrayBuffer) {
var bytes = new Uint8Array(audioArrayBuffer);
$http({
method: 'POST',
url: '/upload',
data: { data: bytes }
}).then(function (data) {
console.log(data);
}, function (error) {
console.log('Call failed with error: ', error);
}
);
}
Java:
@PostMapping("/upload")
@ResponseBody
public void upload(@RequestBody byte[] bytearray) throws UnsupportedAudioFileException {
String fileName ="test"+new Date().getTime()+".wav";
String filePath = rootLocation + fileName;
try {
InputStream b_in = new ByteArrayInputStream(bytearray);
AudioFormat format = new AudioFormat(48000, 16, 1, true, false);
AudioInputStream stream = new AudioInputStream(b_in, format,
bytearray.length);
File file = new File(filePath);
AudioSystem.write(stream,javax.sound.sampled.AudioFileFormat.Type.WAVE, file);
System.out.println("File saved: " + file.getName() + ", bytes: "
+ bytearray.length);
} catch (IOException ex) {
System.err.println(ex);
}
}
Initially I tried sending arraybuffer, which result in a blank wav file. I then changed it to Uint8Array. With this I get a wav file with some noise. Moreover my recording was some 10 seconds audio and the wav file is more than 2 minutes long.
For server side code, change your method to take `"application/json"` as content type and method like
`public void upload(@RequestBody Data data) throws UnsupportedAudioFileException`
where the Data is a simple POJO with one field `byte[] bytes`. – Gautam Aug 24 '17 at 03:59