Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 4.62 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. java audio recording
  2. while (true) {
  3.     System.out.println("recording..");
  4.     recorder.start();
  5.     Thread.sleep(5000);
  6.     recorder.stop();
  7.     Thread.sleep(300);
  8.     System.out.println("recording finished");
  9.  
  10.     data = toolkit.readInputStream(recorder.getInputStream());
  11.  
  12.     pkt = create(data);
  13.  
  14.     outport.send(pkt);
  15. }
  16.        
  17. public class SoundRecorder implements Runnable{
  18.  
  19.  
  20.         final int bufSize = 16384;
  21.  
  22.         AudioInputStream audioInputStream;
  23.         String errStr;
  24.         double duration, seconds;
  25.         File file;
  26.  
  27.      public void start() {
  28.                 errStr = null;
  29.                 thread = new Thread(this);
  30.                 thread.setName("Capture");
  31.                 thread.start();
  32.             }
  33.  
  34.             public void stop() {
  35.                 thread = null;
  36.             }
  37.  
  38.  private void shutDown(String message) {
  39.             if ((errStr = message) != null && thread != null) {
  40.                 thread = null;
  41.                 System.err.println(errStr);
  42.  
  43.             }
  44.         }
  45.  
  46.  
  47.       public void run() {
  48.  
  49.                 duration = 0;
  50.                 audioInputStream = null;
  51.  
  52.  
  53.                 // define the required attributes for our line,
  54.                 // and make sure a compatible line is supported.
  55.  
  56.                 AudioFormat format = getFormat();
  57.                 DataLine.Info info = new DataLine.Info(TargetDataLine.class,
  58.                         format);
  59.  
  60.                 if (!AudioSystem.isLineSupported(info)) {
  61.                     shutDown("Line matching " + info + " not supported.");
  62.                     return;
  63.                 }
  64.  
  65.                 // get and open the target data line for capture.
  66.  
  67.                 try {
  68.                     line = (TargetDataLine) AudioSystem.getLine(info);
  69.                     line.open(format, line.getBufferSize());
  70.                 } catch (LineUnavailableException ex) {
  71.                     shutDown("Unable to open the line: " + ex);
  72.                     return;
  73.                 } catch (SecurityException ex) {
  74.                     shutDown(ex.toString());
  75.  
  76.                     return;
  77.                 } catch (Exception ex) {
  78.                     shutDown(ex.toString());
  79.                     return;
  80.                 }
  81.  
  82.                 // play back the captured audio data
  83.                 ByteArrayOutputStream out = new ByteArrayOutputStream();
  84.                 int frameSizeInBytes = format.getFrameSize();
  85.                 int bufferLengthInFrames = line.getBufferSize() / 8;
  86.                 int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
  87.                 byte[] data = new byte[bufferLengthInBytes];
  88.                 int numBytesRead;
  89.  
  90.                 line.start();
  91.  
  92.                 while (thread != null) {
  93.                     if ((numBytesRead = line.read(data, 0, bufferLengthInBytes)) == -1) {
  94.                         break;
  95.                     }
  96.                     out.write(data, 0, numBytesRead);
  97.                 }
  98.  
  99.                 // we reached the end of the stream.  stop and close the line.
  100.                 line.stop();
  101.                 line.close();
  102.                 line = null;
  103.  
  104.                 // stop and close the output stream
  105.                 try {
  106.                     out.flush();
  107.                     out.close();
  108.                 } catch (IOException ex) {
  109.                     ex.printStackTrace();
  110.                 }
  111.  
  112.                 // load bytes into the audio input stream for playback
  113.  
  114.                 byte audioBytes[] = out.toByteArray();
  115.                 ByteArrayInputStream bais = new ByteArrayInputStream(audioBytes);
  116.                 audioInputStream = new AudioInputStream(bais, format, audioBytes.length / frameSizeInBytes);
  117.  
  118.                 long milliseconds = (long) ((audioInputStream.getFrameLength() * 1000) / format.getFrameRate());
  119.                 duration = milliseconds / 1000.0;
  120.  
  121.  
  122.  
  123.                 try {
  124.                     audioInputStream.reset();
  125.                 } catch (Exception ex) {
  126.                     ex.printStackTrace();
  127.                     return;
  128.                 }
  129.  
  130.  
  131.             }
  132.  
  133.     public AudioInputStream getInputStream() {
  134.             return audioInputStream;
  135.         }
  136.  
  137.     public AudioFormat getFormat() {
  138.  
  139.             AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
  140.             float rate = 44100.0F;
  141.             int sampleSize = 16;
  142.             boolean bigEndian = true;
  143.             int channels = 2;
  144.  
  145.             return new AudioFormat(encoding, rate, sampleSize,
  146.                     channels, (sampleSize / 8) * channels, rate, bigEndian);
  147.  
  148.         }
  149.     }
  150.        
  151. public void stop() {
  152.     Thread t = thread;
  153.     thread = null;// this will cause your thread to exit
  154.     t.join();//this will wait for the thread to exit before returning.
  155. }