将Base64字符串转换为.wav文件

时间:2015-08-24 11:52:52

标签: android

我有一个webservice,它返回一个音频(.wav)文件的base64编码字符串。我试过这段代码:

 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;
    }

此代码创建一个文件但该文件无效。如果我将wav文件转换为base64,然后我使用此代码它可以很好地工作。但它没有处理我在api响应中得到的字符串。字符串没有任何问题,因为iOS版本中的字符串相同。所以任何人都请帮助我。这是我从Web服务响应中获取的字符串。

  

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

2 个答案:

答案 0 :(得分:1)

解码您的回复数据,我们可以看到它实际上是一条错误消息:

<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>

所以你的问题不是写.wav文件,而是你与服务器的通信(或服务器本身的问题)。

答案 1 :(得分:0)

试试这个,这是将base64转换为.wav的示例,反之亦然

File file = new File(Environment.getExternalStorageDirectory() + "/hello-4.wav");
byte[] bytes = FileUtils.readFileToByteArray(file);

String encoded = Base64.encodeToString(bytes, 0);                                       
Utilities.log("~~~~~~~~ Encoded: ", encoded);

byte[] decoded = Base64.decode(encoded, 0);
Utilities.log("~~~~~~~~ Decoded: ", Arrays.toString(decoded));

try
{
    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();
}