Results 1 to 16 of 16
- 06-02-2011, 02:39 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 9
- Rep Power
- 0
How to create wave file from the datainputstream
Need help...
the client has sent the sound array to the servlet in the dataoutput stream.. Now in servlet i have retrieved it using request.getInputStream() and get it into the Datainputstream object.
Now i want to create the .wav file from this byte array.... how would i create the .wav file..
Please help...
- 06-02-2011, 03:38 PM #2
Does that mean your problem posted at Sending sound file to server is solved?
It will help everyone if you keep your discussion in one place, or at least provide links to the other post.
db
- 06-02-2011, 03:40 PM #3
Read the bytes from the byte array and write them to the file.how would i create the .wav file
- 06-02-2011, 03:41 PM #4
Member
- Join Date
- Jun 2011
- Posts
- 9
- Rep Power
- 0
Yes sending sound file is solved. and saved as sound .wav file. But sound not played .. even it is an invalid file ... as the error message displayed by the windows media player on playing the file...
Please Reply..
- 06-02-2011, 03:48 PM #5
Compare the bytes of the received .wav file with the bytes of the source .wav file.
Where are the differences? Did the transmitting code change anything?
- 06-02-2011, 03:50 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Then it either isn't a wav file, or you've read it incorrectly.
You have an inputstream for the uploaded file, and an output stream to the file.
Read from the first, write to the second.
- 06-02-2011, 06:15 PM #7
You're right, it isn't in WAV format, it's a byte array of samples. Check out the OP's other post.
db
- 06-02-2011, 06:30 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
It is?
So how's that supposed to work?
- 06-02-2011, 06:43 PM #9
I guess the OP needs to verify that the sound array is the contents of a .wav file.client has sent the sound array
- 06-03-2011, 09:05 AM #10
Member
- Join Date
- Jun 2011
- Posts
- 9
- Rep Power
- 0
Actually what i have done is Sound is recorded in J2ME application. in the following way :
recordControl.getControl("capture://audio?encoding=pcm");
Then i saved it in a bytearray and then save it to the outputStream and send it directly to the servlet.
In servlet i am using DataInputStream to read the bytes and these bytes are written to the file using fileOutputStream. and wave file is created.
I have'nt used any codecs to create the wave file. Or i have to use AudioInputStream or is there any class to write the wavefile in java...
Please reply
- 06-03-2011, 09:22 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Why are you using a DataInputStream?
Just use the input stream straight, and write the bytes you read out to the file.
I have no idea what recordControl is, or what that is actually doing so maybe you need to read the docs to find out what it is actually producing.
Should that all be correct then it is either the bytearray that has corrupted, or how you are writing that to the outputstream or how your are reading it on the servlet and then writing it out. So some code might be useful, because otherwise we'll be guessing.
Start with the servlet code for reading and writing the data.
- 06-03-2011, 09:29 AM #12
Member
- Join Date
- Jun 2011
- Posts
- 9
- Rep Power
- 0
Ok could i please share some code then....
One more thing what is the difference in the bytes data in case of DataInputStream , InputStream, BytearrayInputStream ....
Please help...
- 06-03-2011, 09:33 AM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
You are simply reading the input stream that the servlet has access to .
That has all the methods needed to read the bytes.
Why bother wrapping it in another input stream?
Of course we still have no code to see exactly what it is you;re doing.
- 06-04-2011, 07:25 AM #14
Member
- Join Date
- Jun 2011
- Posts
- 9
- Rep Power
- 0
This is the code to send the byte array to server from mobile... I am using Apache Tomcat server...
PHP Code:try { String url = "http://124.252.231.22:8080/AudioWeb/AudioServlet"; HttpConnection c = (HttpConnection) Connector.open(url); c.setRequestMethod(HttpConnection.POST); c.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.1"); c.setRequestProperty("Content-Language", "en-US"); DataOutputStream outputStr = c.openDataOutputStream(); outputStr.write(recordedSoundArray); outputStr.flush(); outputStr.close(); DataInputStream is = c.openDataInputStream(); if (is.available() != 0) { int ch; StringBuffer sb = new StringBuffer(); while ((ch = is.read()) != -1) { sb = sb.append((char) ch); } System.out.println(sb); } c.close(); } catch (Exception ex) { ex.printStackTrace(); }
Now Inside servlet i have the following code :
PHP Code:try { DataInputStream dis = new DataInputStream((InputStream) request.getInputStream()); String message = null; byte[] b = new byte[dis.read()]; File f = new File("myfile.wav"); OutputStream ostream = new FileOutputStream(f); int ch=0; while((ch=dis.read())!=-1){ ostream.write(b,0,b.length); ostream.flush(); } ostream.close(); if (!f.exists()) { f.createNewFile(); } response.setContentType("text/plain"); response.setContentLength(message.length()); PrintWriter out = response.getWriter(); out.println(message); dis.close(); out.flush(); out.close(); } finally { }
- 06-04-2011, 02:22 PM #15
Why do you think the variable: recordedSoundArray contains the same byes as a .wav file?
What is this statement supposed to do?
What are these statements supposed to do?Java Code:byte[] b = new byte[dis.read()];
What is the contents of the .wav file after this?Java Code:while((ch=dis.read())!=-1){ ostream.write(b,0,b.length);Last edited by Norm; 06-04-2011 at 02:24 PM.
- 06-05-2011, 11:41 AM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
DataInputStream
By robs in forum Java GamingReplies: 7Last Post: 04-14-2011, 06:27 PM -
DataInputStream to file
By jasondeegan in forum New To JavaReplies: 5Last Post: 06-16-2010, 03:35 PM -
Play wave file out of a jar file
By schliz in forum Advanced JavaReplies: 2Last Post: 12-03-2009, 01:37 PM -
Problem with DataInputStream
By ZivonHunter in forum New To JavaReplies: 1Last Post: 11-21-2008, 05:10 AM -
Wave Sounds
By Doctor Cactus in forum New To JavaReplies: 2Last Post: 10-22-2008, 01:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks