title: Audio recorder problem in java
body: <p>I have a problem while recording the audio. I created a servlet and I modified the java sound API demo code to some extent and finally I can record the audio. The problem is that when I play the audio I can see the total time of the audio stored as 645.45 or something like that, but I have been recording the audio only for a couple of mins. One more problem is the audio is getting saved in the Eclipse directory instead of the project directory.</p>
  
  <p>This is the servlet code.</p>
  
  <pre><code>package com;
  
  import java.io.ByteArrayInputStream;
  import java.io.ByteArrayOutputStream;
  import java.io.File;
  import java.io.IOException;
  import java.io.InputStream;
  
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.sound.sampled.AudioFileFormat;
  import javax.sound.sampled.AudioFormat;
  import javax.sound.sampled.AudioInputStream;
  import javax.sound.sampled.AudioSystem;
  import javax.sound.sampled.DataLine;
  import javax.sound.sampled.Clip;
  import javax.sound.sampled.LineUnavailableException;
  import javax.sound.sampled.SourceDataLine;
  import javax.sound.sampled.TargetDataLine;
  
  public class SoundRecorder extends HttpServlet {
  
      private static final long serialVersionUID = 1L;
  
      static protected boolean running;
      static ByteArrayOutputStream out;
      double fileName = Math.random(); 
      //strFilename = nowLong.toString();
  
  
  
      public SoundRecorder() {
          System.out.println("Filename will be..." + fileName + ".wav");
      }
  
      public void init() {
  
      }
  
      public void destroy() {
  
      }
  
  
      public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {  
          System.out.println("call received..");
          String method = request.getParameter("method");
          System.out.println(method);
          if("record".equalsIgnoreCase(method)) {
              captureAudio(true);
          }
          else if("stop".equalsIgnoreCase(method)) {
              captureAudio(false);    
          }
          else if("play".equalsIgnoreCase(method)) {
              System.out.println("yet to write");
              playAudio();
          }
      }
  
      public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {  
          System.out.println("call received..");
          String method = request.getParameter("method");
          System.out.println(method);
          doGet(request, response);
      }
  
      private void captureAudio(boolean capturing) {
  
  
              File outputFile = new File(fileName + ".wav");
              AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,44100.0F, 16, 2, 4, 44100.0F, false);
  
              DataLine.Info   info = new DataLine.Info(TargetDataLine.class, audioFormat);
              TargetDataLine  targetDataLine = null;
  
              try
              {
                  targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
                  targetDataLine.open(audioFormat);
              }
              catch (LineUnavailableException e)
              {
                  System.out.println("unable to get a recording line");
                  e.printStackTrace();
                  System.exit(1);
              }
  
              AudioFileFormat.Type    targetType = AudioFileFormat.Type.WAVE;
  
              final Recorder recorder = new Recorder(targetDataLine,targetType,outputFile);
  
              System.out.println("Recording...");
              if(capturing){
              recorder.start();
              }
              else {
              recorder.stopRecording();
              }
  
      }
  
  
  
      private void playAudio() {
          try {
              File file = new File(fileName + ".wav");
              AudioInputStream stream  = AudioSystem.getAudioInputStream(file);
              AudioFormat format = stream.getFormat();
              DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat());
              Clip clip = (Clip) AudioSystem.getLine(info);
              clip.open(stream);              
              clip.start();
          } catch (Exception e) {
              System.err.println("Line unavailable: " + e);
              System.exit(-4);
          } 
      }
  }
  </code></pre>
  
  <p>And this is the recorder class</p>
  
  <pre><code>public class Recorder extends Thread {
      private TargetDataLine  m_line;
      private AudioFileFormat.Type m_targetType;
      private AudioInputStream m_audioInputStream;
      private File m_outputFile;
  
      public Recorder(TargetDataLine line,
                       AudioFileFormat.Type targetType,
                       File file)
      {
          m_line = line;
          m_audioInputStream = new AudioInputStream(line);
          m_targetType = targetType;
          m_outputFile = file;
      }
  
      /** Starts the recording.
          To accomplish this, (i) the line is started and (ii) the
          thread is started.
      */
      public void start()
      {
          m_line.start();
          super.start();
      }
  
      /** Stops the recording.
      */
      public void stopRecording()
      {
          m_line.stop();
          m_line.close();
      }
  
      /** Main working method.
      */
      public void run()
      {
              try
              {
                  AudioSystem.write(
                      m_audioInputStream,
                      m_targetType,
                      m_outputFile);
              }
              catch (IOException e)
              {
                  e.printStackTrace();
              }
      }
  
      private static void closeProgram()
      {
          System.out.println("Program closing.....");
          System.exit(1);
      }
  
      private static void out(String strMessage)
      {
          System.out.println(strMessage);
      }
  
  
  }
  </code></pre>