乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      java awt 實(shí)現(xiàn) 錄音 (2)

       77b8 2012-11-14

      package com.company.test3;
      import javax.swing.JFrame;
      import java.awt.event.WindowAdapter;
      import java.awt.event.WindowEvent;
      import java.awt.Toolkit;
      import javax.swing.JPanel;
      import java.awt.event.ActionListener;
      import javax.sound.sampled.AudioInputStream;
      import javax.swing.JButton;
      import java.io.File;
      import java.util.Vector;
      import java.awt.BorderLayout;
      import javax.swing.border.EmptyBorder;
      import javax.swing.border.SoftBevelBorder;
      import javax.swing.BoxLayout;
      import java.awt.Dimension;
      import java.awt.event.ActionEvent;
      import javax.sound.sampled.AudioFileFormat;
      import javax.sound.sampled.AudioFormat;
      import javax.sound.sampled.TargetDataLine;
      import javax.sound.sampled.AudioSystem;
      import java.io.IOException;
      import javax.sound.sampled.DataLine;
      import javax.sound.sampled.LineUnavailableException;
      import java.io.ByteArrayOutputStream;
      import java.io.ByteArrayInputStream;

      /**
       * 創(chuàng)建一個(gè)JPanel錄音文件,存放到d:\\record.wav, 可以播放
       * @author admin
       *
       */
      public class recordFrame extends JPanel implements ActionListener
      {
          /**
           * 注釋內(nèi)容
           */
          private static final long serialVersionUID = 1L;
         
          final int bufSize = 16384;
         
          Capture capture = new Capture();
         
          AudioInputStream audioInputStream;
         
          JButton captB;
         
          String fileName = "untitled";
         
          String errStr;
         
          double duration, seconds;
         
          File file;
         
          Vector lines = new Vector();
         
          public recordFrame()
          {
           //界面風(fēng)格設(shè)定
              setLayout(new BorderLayout());
              EmptyBorder eb = new EmptyBorder(5, 5, 5, 5);
              SoftBevelBorder sbb = new SoftBevelBorder(SoftBevelBorder.LOWERED);
              setBorder(new EmptyBorder(5, 5, 5, 5));
             
              JPanel p1 = new JPanel();
              p1.setBorder(sbb);
              p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));
             
              JPanel buttonsPanel = new JPanel();
              buttonsPanel.setBorder(new EmptyBorder(10, 0, 5, 0));
              captB = addButton("Record", buttonsPanel, true);
              p1.add(buttonsPanel);
             
              add(p1);
          }
         
          public void open()
          {
          }
         
          public void close()
          {
              if (capture.thread != null)
                  captB.doClick(0);
          }
         
          private JButton addButton(String name, JPanel p, boolean state)
          {
              JButton b = new JButton(name);
              b.addActionListener(this);
              b.setEnabled(state);
              p.add(b);
              return b;
          }
         
          public void actionPerformed(ActionEvent e)
          {
              Object obj = e.getSource();
             
              if (obj.equals(captB))
              {
                  if (captB.getText().startsWith("Record"))
                  {
                      file = null;
                      capture.start();
                      fileName = "untitled";
                      captB.setText("Stop");
                  }
                  else
                  {
                      lines.removeAllElements();
                      capture.stop();
                      captB.setText("Record");
                  }
              }
          }
         
          /**
           * 讀取錄音文件,存放到audioInputStream
           * 暫時(shí)未使用。
           * @param file
           * @param updateComponents
           */
          public void createAudioInputStream(File file, boolean updateComponents)
          {
              if (file != null && file.isFile())
              {
                  try
                  {
                      this.file = file;
                      errStr = null;
                      audioInputStream = AudioSystem.getAudioInputStream(file);
                      fileName = file.getName();
                      long milliseconds = (long)((audioInputStream.getFrameLength() * 1000) /

                      audioInputStream.getFormat().getFrameRate());
                      duration = milliseconds / 1000.0;
                      if (updateComponents)
                      {
                      }
                  }
                  catch (Exception ex)
                  {
                      reportStatus(ex.toString());
                  }
              }
              else
              {
                  reportStatus("Audio file required.");
              }
          }
          /**
           * 將當(dāng)前錄音存放到文件
           * @param name
           * @param fileType
           */
          public void saveToFile(String name, AudioFileFormat.Type fileType)
          {
              if (audioInputStream == null)
              {
                  reportStatus("No loaded audio to save");
                  return;
              }
              else if (file != null)
              {
                  createAudioInputStream(file, false);
              }
              // reset to the beginnning of the captured data
              try
              {
                  audioInputStream.reset();
              }
              catch (Exception e)
              {
                  reportStatus("Unable to reset stream " + e);
                  return;
              }
              File file = new File(fileName = name);
              try
              {
                  if (AudioSystem.write(audioInputStream, fileType, file) == -1)
                  {
                      throw new IOException("Problems writing to file");
                  }
              }
              catch (Exception ex)
              {
                  reportStatus(ex.toString());
              }
          }
         
          private void reportStatus(String msg)
          {
              if ((errStr = msg) != null)
              {
                  System.out.println(errStr);
              }
          }
         
          /**
           * 錄音核心模塊,管理類
           * @author admin
           *
           */
          class Capture implements Runnable
          {
              TargetDataLine line;
             
              Thread thread;
             
              public void start()
              {
                  errStr = null;
                  thread = new Thread(this);
                  thread.setName("Capture");
                  thread.start();
              }
             
              public void stop()
              {
                  thread = null;
              }
             
              private void shutDown(String message)
              {
                  if ((errStr = message) != null && thread != null)
                  {
                      thread = null;
                      captB.setText("Record");
                      System.err.println(errStr);
                  }
              }
             
              public void run()
              {
                  duration = 0;
                  audioInputStream = null;
                 
                  // get an AudioInputStream of the desired format for playback
                  AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
                 
                  // define the required attributes for our line,
                  // and make sure a compatible line is supported.
                  //            float rate = 44100f; 8000、11025、16000、22050、44100
                  //            int sampleSize = 16; 8, 16
                  //            String signedString = "signed";
                  //            boolean bigEndian = true;
                  //            int channels = 2;
                  float rate = 192000;
                  int sampleSize = 16;
                  boolean bigEndian = true;
                  int channels = 1;
                  AudioFormat format =
                      new AudioFormat(encoding, rate, sampleSize, channels, (sampleSize / 8) * channels, rate, bigEndian);
                 
                  DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
                 
                  if (!AudioSystem.isLineSupported(info))
                  {
                      shutDown("Line matching " + info + " not supported.");
                      return;
                  }
                 
                  // get an AudioInputStream of the desired format for playback
                  try
                  {
                      line = (TargetDataLine)AudioSystem.getLine(info);
                      line.open(format, line.getBufferSize());
                  }
                  catch (LineUnavailableException ex)
                  {
                      shutDown("Unable to open the line: " + ex);
                      return;
                  }
                  catch (SecurityException ex)
                  {
                      shutDown(ex.toString());
                      return;
                  }
                  catch (Exception ex)
                  {
                      shutDown(ex.toString());
                      return;
                  }
                 
                  // play back the captured audio data
                  ByteArrayOutputStream out = new ByteArrayOutputStream();
                  int frameSizeInBytes = format.getFrameSize();
                  int bufferLengthInFrames = line.getBufferSize() / 8;
                  int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
                  byte[] data = new byte[bufferLengthInBytes];
                  int numBytesRead;
                 
                  line.start();
                  while (thread != null)
                  {
                      if ((numBytesRead = line.read(data, 0, bufferLengthInBytes)) == -1)
                      {
                          break;
                      }
                      out.write(data, 0, numBytesRead);
                  }
                 
                  // we reached the end of the stream. stop and close the line.
                  line.stop();
                  line.close();
                  line = null;
                 
                  // stop and close the output stream
                  try
                  {
                      out.flush();
                      out.close();
                  }
                  catch (IOException ex)
                  {
                      ex.printStackTrace();
                  }
                 
                  // load bytes into the audio input stream for playback
                  byte audioBytes[] = out.toByteArray();
                  ByteArrayInputStream bais = new ByteArrayInputStream(audioBytes);
                  audioInputStream = new AudioInputStream(bais, format, audioBytes.length / frameSizeInBytes);
                  long milliseconds = (long)((audioInputStream.getFrameLength() * 1000) / format.getFrameRate());
                  duration = milliseconds / 1000.0;
                  try
                  {
                      audioInputStream.reset();
                  }
                  catch (Exception ex)
                  {
                      ex.printStackTrace();
                      return;
                  }
                  saveToFile("d:\\record2.wav", AudioFileFormat.Type.WAVE);
              }
          }
         
          public static void main(String[] args)
          {
              recordFrame test = new recordFrame();
              test.open();
              JFrame f = new JFrame("Capture");
              f.addWindowListener(new WindowAdapter()
              {
                  public void windowClosing(WindowEvent e)
                  {
                      System.exit(0);
                  }
              });
              f.getContentPane().add("Center", test);
              f.pack();
              Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
              int w = 720;
              int h = 340;
              f.setLocation(screenSize.width / 2 - w / 2, screenSize.height / 2 - h / 2);
              f.setSize(w, h);
              f.show();
          }
      }

       

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約