title: Java Record Mic to Byte Array and Play sound
body: <p>I want to make a live voice chatting program in Java, but I know nothing about recording/playing sound in Java, so with the help of Google, I think I have been able to record from my mic to a byte array with the following:</p>
  
  <pre><code>AudioFormat format = new AudioFormat(8000.0f, 16, 1, true, true);
  TargetDataLine microphone;
  try{
      microphone = AudioSystem.getTargetDataLine(format);
  
      DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
      microphone = (TargetDataLine)AudioSystem.getLine(info);
      microphone.open(format);
  
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      int numBytesRead;
      byte[] data = new byte[microphone.getBufferSize()/5];
      microphone.start();
  
      int bytesRead =0;
  
      try{
          while(bytesRead&lt;100000){ //Just so I can test if recording my mic works...
              numBytesRead = microphone.read(data, 0, data.length);
              bytesRead = bytesRead + numBytesRead;
          //    System.out.println(bytesRead);
              out.write(data, 0, numBytesRead);
          }
      catch(Exception e){
          e.printStackTrace();
      }
      microphone.close();
  
  catch(LineUnavailibleException e){
      e.printStackTrace();
  }
  </code></pre>
  
  <p>So now, to my understanding, if I call out.toByteArray();, I should have gotten a byte array of the sound I just recorded from my microphone. (I got no errors running the above, but have no way to prove if it actually recorded because I do not wish to output it to a file and didn't do so)</p>
  
  <p>Now, if the above is correct, then below is where I run into my problem:
  I want to now play the byte array I just created... (In my real program, I would've sent the bytes over to my "receiving program" through a Java socket which I have already been able to do, but right now I just want to make a small program that records the mic and plays it back). In order to play the sound information from the byte array I followed this: <a href="http://www.wikijava.org/wiki/Play_a_wave_sound_in_Java" rel="noreferrer">http://www.wikijava.org/wiki/Play_a_wave_sound_in_Java</a>
  And came up with the following: (this is located right after microphone.close() from the above)</p>
  
  <pre><code>try{
      DataLine.Info info2 = DataLine.Info(SourceDataLine.class, format);
      SourceDataLine dataLine = (SourceDataLine)AudioSystem.getLine(info2);
      int bufferSize = 2200;
      soundLine.open(format, bufferSize);
      soundLine.start();
      AudioInputStream audioInputStream = null;
  
      InputStream input = new ByteArrayInputStream(out.toByteArray());
      audioInputStream = AudioSystem.getAudioInputStream(input);
  
      ...
  </code></pre>
  
  <p>The rest is pretty much copy pasted from the playSound.java from this link:
  <a href="http://www.wikijava.org/wiki/Play_a_wave_sound_in_Java" rel="noreferrer">http://www.wikijava.org/wiki/Play_a_wave_sound_in_Java</a></p>
  
  <p>When I run the above code... The recording seems to work all right, but I get the following error:    </p>
  
  <p><code>javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream</code></p>
  
  <p>for this line <code>audioInputStream = AudioSystem.getAudioInputStream(input);</code></p>
  
  <p>From my limited knowledge, I'm assuming it's because I somehow messed up the recording method, I need some sort of "Audio Format Header?" <a href="https://ccrma.stanford.edu/courses/422/projects/WaveFormat/" rel="noreferrer">https://ccrma.stanford.edu/courses/422/projects/WaveFormat/</a> (I assumed I wouldn't need something like that because I never saved to a file and just kept it all as a  byte array), or I just completely misunderstood how java's AudioInputStream reads and parses data... </p>
  
  <p>This is my first time working with any sound related things in Java, so I apologize if I am completely misunderstanding and butchering this code (yeah I know the code looks pretty bad and unorganized but I just want to get it to work)... I tried multiple searches on Google/StackOverflow, and was able to find a very similar question:</p>
  
  <p><a href="https://stackoverflow.com/questions/12589156/java-byte-array-play-sound">java byte array play sound</a></p>
  
  <p>but it was also unanswered (the only answer was to save it to a file, but what we both want is to stream it directly as a byte array without it ever becoming a file)</p>
  
  <p>What I do know:</p>
  
  <p>Audio can be recorded using a TargetDataLine, and recording the microphone, which can be outputted to a Byte Array using a ByteArrayOutputStream</p>
  
  <p>Audio can be saved to a file and played by using a AudioInputStream to read the file and a SourceDataLine to play the data. </p>
  
  <p>If I wanted to write a file, I could use AudioSystem.write(new AudioInputStream(microphone), AudioFileFormat.Type.WAVE, new File("recording.wav"); //I have tested this by replacing the while loop with this line and it recorded fine (except it would never stop so I had to manually terminate it), but I don't want that, because outputting to a file means it will be impossible to send it over a socket to another side in real time.</p>
  
  <p>What I don't know / My Question: </p>
  
  <p>How to record and stream audio recorded from a mic to another computer that can be played with as little delay as possible (pretty much like a voice chat similar to Skype) with Java. </p>
  
  <p>Thanks in advance for any help or someone who can point me in the correct direction. Also if someone knows a simpler method then please tell me that as well. </p>