我正在尝试从我的Android应用程序发送一个.wav文件到Django服务器。主要的问题是在服务器端经常出现这样的错误:波。错误:文件不以RIFF id开头
从客户端的角度来看,这是我转换测试的方法_音频.wav文件到字节[]HashMap postParams = new HashMap<>();
InputStream inStream = testPronunciationView.getContext().getResources().openRawResource(R.raw.test_audio);
ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedInputStream in = new BufferedInputStream(inStream);
int read;
byte[] buff = new byte[1024];
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
out.flush();
byte[] fileAudioByte = out.toByteArray();
// two options to transform in a string
// 1st option
String decoded = new String(fileAudioByte, "UTF-8");
// 2nd option
String decoded = toJSON(fileAudioByte);
// decoded = either one of above
postDataParams.put("Audio", decoded)
// ....
// prepare a POST request here to send to the server
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
编辑:创建JSON字符串的方法:
^{pr2}$
在服务器端我需要:audiofile_string = data['FileAudio']
audiofile_byte = list(bytearray(audiofile_string, 'utf8'))
temp_audiofile = tempfile.NamedTemporaryFile(suffix='.wav')
with open(temp_audiofile.name, 'wb') as output:
output.write(''.join(str(v) for v in audiofile_byte))
# The following line throws the error
f = wave.open(temp_audiofile.name, 'r') # wave.py library
所以我认为我在转换或是电话后做了些错事。有什么建议吗?谢谢