Nitish Nitish - 5 months ago 125
Android Question

Convert Base64 String into .wav file

I have a webservice that returns a base64 encoded string of an audio

(.wav)
file. I have tried this code:

private String base64StringToFile(String base64AudioData) {

String fullPath = null;

byte[] decoded = Base64.decode(base64AudioData,1);
//byte[] decoded = Base64.decode(encoded, 0);
Log.e("~~~~~~~~ Decoded: ", Arrays.toString(decoded));

try {
fullPath = FileManger.getttsSavePath()+"/"+("fTts.wav");
File file2 = new File(fullPath);
//File file2 = new File(Environment.getExternalStorageDirectory() + "/hello-5.wav");
FileOutputStream os = new FileOutputStream(file2, true);
os.write(decoded);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
return fullPath;
}


This code creates a file but the file is not working. If I convert a
wav
file into base64 and then I use this code it works very well. But it's not working on the String which I am getting in the
api
response. There is nothing wrong with the string because the same string is working in the
iOS
version. So anyone please help me on it. This is the string which I am getting from the web service response.


PGh0bWw+CjxoZWFkPgo8bWV0YSBodHRwLWVxdWl2PSJDb250ZW50LVR5cGUiIGNvbnRlbnQ9InRleHQvaHRtbDsgY2hhcnNldD1VVEYtOCIvPgo8dGl0bGU+RXJyb3IgNDA2IHgtbnVhbmNlLXNlc3Npb25pZDE4ZWM1ZTQ3LTE4OGEtNGEyMS1hNWVjLWM3YjBjMWNlNjYyOApSZWNlaXZlZCBRdWVyeUVycm9yOiA1MTEgVm9pY2Ugbm90IGF2YWlsYWJsZS4gRm9yIGEgbGlzdCBvZiBhdmFpbGFibGUgdm9pY2VzLCBwbGVhc2Ugc2VlIHRoZSBEcmFnb24gTW9iaWxlIFNESyB3ZWJzaXRlLjwvdGl0bGU+CjwvaGVhZD4KPGJvZHk+PGgyPkhUVFAgRVJST1IgNDA2PC9oMj4KPHA+UHJvYmxlbSBhY2Nlc3NpbmcgL05NRFBUVFNDbWRTZXJ2bGV0L3R0cy4gUmVhc29uOgo8cHJlPiAgICB4LW51YW5jZS1zZXNzaW9uaWQxOGVjNWU0Ny0xODhhLTRhMjEtYTVlYy1jN2IwYzFjZTY2MjgKUmVjZWl2ZWQgUXVlcnlFcnJvcjogNTExIFZvaWNlIG5vdCBhdmFpbGFibGUuIEZvciBhIGxpc3Qgb2YgYXZhaWxhYmxlIHZvaWNlcywgcGxlYXNlIHNlZSB0aGUgRHJhZ29uIE1vYmlsZSBTREsgd2Vic2l0ZS48L3ByZT48L3A+CjwvYm9keT4KPC9odG1sPgo=

Answer

Decoding your response data, we can see that it's actually an error message:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Error 406 x-nuance-sessionid18ec5e47-188a-4a21-a5ec-c7b0c1ce6628
Received QueryError: 511 Voice not available. For a list of available voices, please see the Dragon Mobile SDK website.</title>
</head>
<body><h2>HTTP ERROR 406</h2>
<p>Problem accessing /NMDPTTSCmdServlet/tts. Reason:
<pre>    x-nuance-sessionid18ec5e47-188a-4a21-a5ec-c7b0c1ce6628
Received QueryError: 511 Voice not available. For a list of available voices, please see the Dragon Mobile SDK website.</pre></p>
</body>
</html>

So your problem is not writing the .wav file, but rather your communication with the server (or a problem with the server itself).

Comments