13 Replies Latest reply: Sep 19, 2010 7:19 PM by 796367 RSS

    datainputstream

    csckid
      What does byte[] b
      DatainputStream has a method read(byte[] b, int off, int len)
      What does byte[] b indicates?
      is it like data is stored in byte[] b from the inputstream?
      byte[] data;
      int ch=0;
       while ((ch = response.read()) != -1) {
              
        }
      can this ch be converted into byte? Here input stream is reading a mp3 file
        • 1. Re: datainputstream
          Kayaman
          csckid wrote:
          DatainputStream has a method read(byte[] b, int off, int len)
          What does byte[] b indicates?
          is it like data is stored in byte[] b from the inputstream?
          Yes, it's the buffer where you want data to be read.
          byte[] data;
          int ch=0;
          while ((ch = response.read()) != -1) {
          
          }
          can this ch be converted into byte? Here input stream is reading a mp3 file
          Yes it can, but you don't really want to read one single byte at a time. Especially since you seem to know there are methods capable of reading more than one byte.
          • 2. Re: datainputstream
            EJP
            What part of the Javadoc don't you understand?
            • 3. Re: datainputstream
              csckid
              I have a file of 200KB.
              Is it possible to download first 78KB and play the file?
              Well I tried and was out of success.
              Here is the code:
              HttpConnection c = (HttpConnection) Connector.open("http://localhost/uploads/file.wav"); 
              
                   DataInputStream response = new DataInputStream(c.openInputStream());
                  byte[] buffer = new byte[80000];
                   int read = -1;
              ByteArrayOutputStream byteout = new ByteArrayOutputStream();
                   while ((read = response.read(buffer)) >= 0)
                   {
                        byteout.write(buffer, 0, read);
                        playnow(byteout);
                               break;
                                                 
                   }
                        
                      c.close();
                    response.close();
              • 4. Re: datainputstream
                Kayaman
                csckid wrote:
                Is it possible to download first 78KB and play the file?
                Depends on the file format, could work on wavs.
                Well I tried and was out of success.
                You're not downloading the first 78KB of the file. You're downloading the whatever amount of bytes that you get from the first read and trying to play that.
                Put a System.out.println("bytes read: " + read); in your "loop" and you'll see the amount of bytes that you're attempting to play.
                • 5. Re: datainputstream
                  csckid
                  My file isn't played
                  Here is the code for playnow function:
                    public synchronized void playnow(ByteArrayOutputStream b){
                               try{
                                ByteArrayInputStream is=new ByteArrayInputStream(b.toByteArray());
                                
                                Player p2 = Manager.createPlayer(is,"audio/x-wav"); //change here while run
                  
                                 p2.start();
                               }catch(Exception e){
                                    System.out.println(e);
                               }
                                      
                        }
                  • 6. Re: datainputstream
                    Kayaman
                    csckid wrote:
                    My file isn't played
                    And did you read my reply where I explained why not?
                    • 7. Re: datainputstream
                      csckid
                      The file runs.
                      Depends on the file format, could work on wavs.
                      Will amr file work
                      • 8. Re: datainputstream
                        Kayaman
                        csckid wrote:
                        The file runs.
                        Depends on the file format, could work on wavs.
                        Will amr file work
                        Why do you have to ask everything separately, instead of trying things yourself, which would be a lot faster too?
                        • 9. Re: datainputstream
                          csckid
                          ok, I will try to avoid that in the future.

                          New problem.
                          I have a file of 34MB
                          Is it possible to play the file which has been obtained by two inputstream.read()? and then play the next two inputstream.read() of the file.

                          I was able to play the first two read. But when I try to play the next two read it says
                          javax.microedition.media.MediaException: Malformed wave media

                          Here is the code:
                          function 1:
                               HttpConnection c = (HttpConnection) Connector.open("http://localhost//test.wav"); 
                               DataInputStream response = new DataInputStream(c.openInputStream());
                               byte[] buffer = new byte[100000];
                               int read = -1;
                               ByteArrayOutputStream byteout = new ByteArrayOutputStream();
                               int times=0;
                               while ((read = response.read(buffer)) >= 0)
                               {
                                  byteout.write(buffer, 0, read);
                                 System.out.println("bytes read: " + read); 
                                                             
                                     times++;
                                  if(times==2){
                                    byteout.reset();
                                  }
                                  if(times==4){
                                         System.out.println("bytes stream: " + byteout.size()); 
                                         laynow(byteout);
                                         break;
                                  }
                                                             
                                                        
                             }
                          public synchronized void playnow(ByteArrayOutputStream b){
                                       try{
                                           ByteArrayInputStream is=new ByteArrayInputStream(b.toByteArray());
                                                       
                                           Player p2 = Manager.createPlayer(is,"audio/x-wav"); //change here while run
                                            p2.prefetch();
                                              p2.start();
                                       }catch(Exception e){
                                            System.out.println(e);
                                       }
                                              
                                }
                          Is it possible to do in this way? or do I need to chop the file and play each file sequentially.
                          • 10. Re: datainputstream
                            Kayaman
                            csckid wrote:
                            New problem.
                            I have a file of 34MB
                            Is it possible to play the file which has been obtained by two inputstream.read()? and then play the next two inputstream.read() of the file.

                            I was able to play the first two read. But when I try to play the next two read it says
                            javax.microedition.media.MediaException: Malformed wave media
                            You should always let people know that you're using Java ME. Otherwise people will assume you're using the SE.

                            As for your problems. Only raw file formats can be played like that. Any more advanced format is bound to have header data, chunk data and what not.
                            You can't just read random amount of bytes like that.

                            It might be possible to read in smaller chunks that keep the data format intact and play those out. But then you'd need to read the documentation for the format and get some code that would parse those chunks for you.
                            • 11. Re: datainputstream
                              csckid
                              you'd need to read the documentation for the format and get some code that would parse those chunks for you.
                              So what will I google for? like "wav parse"?
                              • 12. Re: datainputstream
                                796367
                                Kayaman wrote:
                                Why do you have to ask everything separately, instead of trying things yourself, which would be a lot faster too?
                                This is like trying to patch a small hole with a bigger one.
                                • 13. Re: datainputstream
                                  Darryl Burke
                                  Kayaman wrote:
                                  You should always let people know that you're using Java ME. Otherwise people will assume you're using the SE.
                                  @csckid: There's a [CLDC and MIDP forum|http://forums.sun.com/forum.jspa?forumID=76] for Java ME problems. Since this discussion has gone so far, I'm not moving this thread. In future please post any Java ME question in the appropriate mobility forum.

                                  db