title: Send audio over UDP
body: <p>So, I was looking for a microphone data sending tut, but I haven't found any.
  So I read Oracles tut about line opening and I am able to record the audio to a ByteArrayOutputStream, but now I have 2 problems!</p>
  
  <p>First:
  How to play the recorded audio.</p>
  
  <p>Second: if I am recording it to a BAOS how would i dynamically send it.
  I suppose I would send the data array, but would it be too processor hoggy to write to a BAOS every time I recieve it, or could I do it differently?</p>
  
  <p>Current code:</p>
  
  <pre><code>import java.io.ByteArrayOutputStream;
  
  import javax.sound.sampled.AudioFormat;
  import javax.sound.sampled.AudioSystem;
  import javax.sound.sampled.DataLine;
  import javax.sound.sampled.LineUnavailableException;
  import javax.sound.sampled.TargetDataLine;
  
  public class MicrophoneRecorder {
      static boolean stopped = false;
  
      public static void main(String[] args) {
          AudioFormat format = new AudioFormat(8000.0f, 16, 1, true, true);
          TargetDataLine line = null;
          DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
          if (!AudioSystem.isLineSupported(info)) {
              System.out.println("Not supported!");
          }
          try {
              line = (TargetDataLine) AudioSystem.getLine(info);
              line.open(format);
          } catch (LineUnavailableException ex) {
              ex.printStackTrace();
          }
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          int numBytesRead = 0;
          byte[] data = new byte[line.getBufferSize() / 5];
          line.start();
  
          new Thread(new Runnable() {
  
              @Override
              public void run() {
                  try {
                      Thread.sleep(3000);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  stopped = true;
              }
          }).start();
          while (!stopped) {
              numBytesRead = line.read(data, 0, data.length);
              out.write(data, 0, numBytesRead);
          }
      }
  
  }
  </code></pre>
  
  <p>Thanks for any help given.
  Sincerely, Roberto Anić Banić</p>
  
  <p>P.S.
  Seen this, doesn't work <a href="http://javasolution.blogspot.com/2007/04/voice-chat-using-java.html" rel="nofollow">http://javasolution.blogspot.com/2007/04/voice-chat-using-java.html</a>
  P.P.S.
  Is UDP a good soulution or should I use RTSP</p>