音频问题
在我的 Android 应用程序中,我可以录制音频并将其保存在手机/sdk 上。我检查过在手机上播放时声音清晰。它创建的音频文件大小为5.9kb(.amr格式)。 接下来我将文件上传到服务器,它将音频存储在 SQL 数据库上。上传成功。当播放上传的音频时,都是乱码...
在数据库中,我将音频存储在数据类型为图像的列中,长度为16。
我的问题是..为什么上传后噪音会乱码。如何验证音频是否正确保存且没有添加任何噪音。
文件上传代码
InputStream = new DataInputStream(new FileInputStream( FileName));
byte[] responseData = new byte[10000];
int length = 0;
StringBuffer rawResponse = new StringBuffer();
while (-1 != (length = InputStream.read(responseData)))
rawResponse.append(new String(responseData, 0, length));
String finalstring = rawResponse.toString();
voicedataArray = finalstring.getBytes();
In my android app I can record audio and save it on the phone/sdk. I checked that it is audible and clear when i play it back on the phone. The size of the audio file it created is 5.9kb(.amr format).
Next i upload the file to the server, it stores the audio on sql db. The upload is successful. When the uploaded audio is played, it is all garbled...
In the database i store the audio in a column with datatype image and is of length 16.
My question is ..why is the noise garbled after upload. How do i verify that the audio is saved correctly without any noise added.
Code for file upload
InputStream = new DataInputStream(new FileInputStream( FileName));
byte[] responseData = new byte[10000];
int length = 0;
StringBuffer rawResponse = new StringBuffer();
while (-1 != (length = InputStream.read(responseData)))
rawResponse.append(new String(responseData, 0, length));
String finalstring = rawResponse.toString();
voicedataArray = finalstring.getBytes();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题很可能是由于使用 StringBuffer 来缓冲响应造成的。 Java中的字符是对应于Unicode字符点的两字节实体。 String#getBytes() 的文档说:
因此,不能保证您传入、转换为字符、然后返回字节的字节与您首先传递的流相同。
我认为您需要使用动态扩展字节缓冲区代替 StringBuffer 来编写解决方案。
另外,关于 StringBuffer 的使用还有两个注意事项:
1) 对 StringBuffer 的所有访问都是同步的,因此您会付出性能损失。 StringBuilder 是一个现代的替代品,它不在幕后进行同步。
2) 每次追加到 StringBuffer 时:
您都在分配一个新字符串并将其丢弃。这实在是对垃圾收集器的侮辱。 StringBuffer实际上有一种append()的形式,它会直接接受一个char数组,因此不需要使用中间String。 (但是您可能一开始就不想使用 StringBuffer)。
Your problem is very much likely due to the use of StringBuffer to buffer the response. A character in Java is a two-byte entity corresponding to a Unicode character point. The documentation for String#getBytes() says:
So there's no guarantee that the bytes you are passing in, being converted to characters, then back to bytes is the same stream you passed in the first place.
I think you would need to code your solution using a dynamically expanding byte buffer in place of the StringBuffer.
Also, two notes about the usage of StringBuffer:
1) All accesses to the StringBuffer are synchronized, so you're paying a performance penalty. StringBuilder is a modern-day replacement that doesn't do synchronization under the hood.
2) Each time you append to the StringBuffer:
you are allocating a new string and throwing it away. That's really abusive to the garbage collector. StringBuffer actually has a form of
append()
that will directly take a char array, so there is no need to use an intermediate String. (But you probably don't want to use a StringBuffer in the first place).