title: How to get Audio for encoding using Xuggler
body: <p>I'm writing an application that records the screen and audio. While the screen recording works perfectly, I'm having difficulty in getting the raw audio using the JDK libraries. Here's the code:</p>
  
  <pre><code>try {
              // Now, we're going to loop
              long startTime = System.nanoTime();
  
              System.out.println("Encoding Image.....");
              while (!Thread.currentThread().isInterrupted()) {
                  // take the screen shot
                  BufferedImage screen = robot.createScreenCapture(screenBounds);
  
  
                  // convert to the right image type
                  BufferedImage bgrScreen = convertToType(screen,
                          BufferedImage.TYPE_3BYTE_BGR);
  
                  // encode the image
                  writer.encodeVideo(0, bgrScreen, System.nanoTime()
                          - startTime, TimeUnit.NANOSECONDS);
  
                  /* Need to get audio here and then encode using xuggler. Something like 
  
                      WaveData wd = new WaveData();
  
                      TargetDataLine line;
                      AudioInputStream aus = new AudioInputStream(line);
  
                      short[] samples = getSourceSamples();
                         writer.encodeAudio(0, samples); */
  
  
                  if (timeCreation &lt; 10) {
                      timeCreation = getGMTTime();
                  }
                  // sleep for framerate milliseconds
                  try {
                      Thread.sleep((long) (1000 / FRAME_RATE.getDouble()));
                  } catch (Exception ex) {
                      System.err.println("stopping....");
                      break;
                  }
  
              }
              // Finally we tell the writer to close and write the trailer if
              // needed
          } finally {
              writer.close();
          }
  </code></pre>
  
  <p>This <a href="http://www.xuggle.com/public/documentation/java/api/com/xuggle/mediatool/IMediaWriter.html" rel="nofollow noreferrer">page</a> has some pseudo code like</p>
  
  <pre><code>while(haveMoreAudio())
   {
     short[] samples = getSourceSamples();
     writer.encodeAudio(0, samples);
   }
  </code></pre>
  
  <p>but what exactly should I do for <code>getSourceSamples()</code>?</p>
  
  <p>Also, a bonus question - is it possible to choose from multiple microphones in this approach?</p>
  
  <p>See also:
  <a href="https://stackoverflow.com/questions/13839850/xuggler-encoding-and-muxing/13934751?noredirect=1#comment32570748_13934751">Xuggler encoding and muxing</a></p>