Results 1 to 16 of 16
  1. #1
    rajan701v is offline Member
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

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

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,232
    Rep Power
    19

    Default

    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

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default

    how would i create the .wav file
    Read the bytes from the byte array and write them to the file.

  4. #4
    rajan701v is offline Member
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default

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

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default

    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?

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,001
    Rep Power
    20

    Default

    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.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,232
    Rep Power
    19

    Default

    You're right, it isn't in WAV format, it's a byte array of samples. Check out the OP's other post.

    db

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,001
    Rep Power
    20

    Default

    It is?
    So how's that supposed to work?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default

    client has sent the sound array
    I guess the OP needs to verify that the sound array is the contents of a .wav file.

  10. #10
    rajan701v is offline Member
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default

    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

  11. #11
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,001
    Rep Power
    20

    Default

    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.

  12. #12
    rajan701v is offline Member
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default

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

  13. #13
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,001
    Rep Power
    20

    Default

    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.

  14. #14
    rajan701v is offline Member
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Question

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

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default

    Why do you think the variable: recordedSoundArray contains the same byes as a .wav file?

    What is this statement supposed to do?
    Java Code:
    byte[] b = new byte[dis.read()];
    What are these statements supposed to do?
    Java Code:
     while((ch=dis.read())!=-1){
                    ostream.write(b,0,b.length);
    What is the contents of the .wav file after this?
    Last edited by Norm; 06-04-2011 at 02:24 PM.

  16. #16
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,001
    Rep Power
    20

    Default

    As Norm is impllying, your code is all over the place.
    Did you refer to the API for output streams, because if so you've clearly misunderstood something.

Similar Threads

  1. DataInputStream
    By robs in forum Java Gaming
    Replies: 7
    Last Post: 04-14-2011, 06:27 PM
  2. DataInputStream to file
    By jasondeegan in forum New To Java
    Replies: 5
    Last Post: 06-16-2010, 03:35 PM
  3. Play wave file out of a jar file
    By schliz in forum Advanced Java
    Replies: 2
    Last Post: 12-03-2009, 01:37 PM
  4. Problem with DataInputStream
    By ZivonHunter in forum New To Java
    Replies: 1
    Last Post: 11-21-2008, 05:10 AM
  5. Wave Sounds
    By Doctor Cactus in forum New To Java
    Replies: 2
    Last Post: 10-22-2008, 01:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •