1

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();
jsp
  • 2,546
  • 5
  • 36
  • 63

1 Answers1

2

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:

Returns a new byte array containing the characters of this string encoded using the system's default charset.

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:

rawResponse.append(new String(responseData, 0, length));

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

mportuesisf
  • 5,587
  • 2
  • 33
  • 26
  • i modified it to do something like this http://www.exampledepot.com/egs/java.io/File2ByteArray.html...but that also didnt work – jsp Jul 14 '11 at 23:47
  • Did it fail in the same way as your original example? Are you using the code from that example verbatim, or did you adapt it in some way? – mportuesisf Jul 14 '11 at 23:57
  • used it verbatim ...got the same result – jsp Jul 15 '11 at 01:58
  • 1
    Have you checked the database schema for your SQL DB. It should be storing the audio in a binary blob. And the code on the server handling the audio needs to pass the bytes through untouched, as well. – mportuesisf Jul 15 '11 at 16:22
  • Yes, I 'm storing the bytes passed as Blob. – jsp Jul 15 '11 at 16:48
  • Then I suspect a few more things could be at work here. Here are the possibilities off the top of my head: 1) The Android code that's doing the upload to the server is corrupting the data. If you are uploading via HTTP, you should be doing a multipart/form-data upload, with the binary data as the body of the upload. Otherwise, you're uploading the data as if it were text. 2) The code on the server that is processing the upload is corrupting the data before it gets stored in the DB. You could test that by writing the data to a file on the server, then trying to play the file. – mportuesisf Jul 15 '11 at 17:03
  • @mportuesisf let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1518/discussion-between-jayp-and-mportuesisf) – jsp Jul 15 '11 at 17:11
  • Fixing the code for audio file to bytes resolved the issue. Apparently the bytes were getting garbled therefore was producing noise when played back at the server.. used this code to convert file to bytes http://stackoverflow.com/questions/858980/file-to-byte-in-java/859233#859233 – jsp Jul 15 '11 at 18:06