java Stream Audio

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hansun11
    New Member
    • Sep 2014
    • 2

    java Stream Audio

    I want to use a ServerSocket at a PC
    and a Socket for users
    And I tried to Stream a music from Server to Client
    But I dont know what should I send to the Client
    Here's part of my code
    Code:
    public static void CP(File media , int LoopCount)
       {
          AudioInputStream AIS = null;
          try
          {
             AIS = AudioSystem.getAudioInputStream(media);
          }
          catch(Exception e)
          {
             e.printStackTrace();
          }
          if (AIS != null)
          {
            AudioFormat	format = AIS.getFormat();
            System.out.println(format.toString());
            SourceDataLine line = null ;
            try
            {
               line = AudioSystem.getSourceDataLine(format);
            }
            catch(Exception ex){}
            
            byte[]	abData = new byte[128000];
            int readByte = 0;
            while(readByte != -1)
            {
                try
                {              
                  readByte = AIS.read(abData, 0, abData.length);
                  ops.write(new byte[readByte]);
                  if(readByte > 0)
                  {
                     line.open(format);
                     line.start();              
                     int lineWritten = line.write(abData,0,readByte);
                     //System.out.println(readByte);
                  }
                }
                catch (LineUnavailableException e)
                {
                  e.printStackTrace();
                }
                catch (Exception e)
                {
                  e.printStackTrace();
                }
            }
          }
       }
    What should I send? the <readByte> ? or the <lineWritten>
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    Send the <readByte>.
    The <lineWritten> is just an iteger that tells you about the success of your data sent (there may be an error)

    Comment

    • hansun11
      New Member
      • Sep 2014
      • 2

      #3
      Well .. I tried I sent <readByte> to the client
      Then here'S another question. Don't know how to use it
      To play an audio need a inputstream a format and a line.
      But to new a stream needs a file or url. And format needs stream line needs format
      But I only got a readbyte . So i have no idea how to use it
      I''ll paste on the code of the client later

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        Use ByteArrayInputS tream. This stream only needs bytes, no file or URL.

        Comment

        Working...