有问题的java录音并显示波形的小程序,求大神指点

package yun;


import java.awt.Color;

//import java.awt.Dimension;

//import java.awt.FlowLayout;

import java.awt.Graphics;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

//import java.awt.event.WindowAdapter;

//import java.awt.event.WindowEvent;

//import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

//import java.io.IOException;

//import java.io.InputStream;


import javax.sound.sampled.AudioFormat;

//import javax.sound.sampled.AudioInputStream;

import javax.sound.sampled.AudioSystem;

import javax.sound.sampled.DataLine;

import javax.sound.sampled.LineUnavailableException;

//import javax.sound.sampled.SourceDataLine;

import javax.sound.sampled.TargetDataLine;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;


public  class audio extends JFrame {

//取得屏幕的宽度

int  width=Toolkit.getDefaultToolkit().getScreenSize().width;

//取得屏幕的高度

int height =Toolkit.getDefaultToolkit().getScreenSize().height;

   //设置一个按钮用于监听声音

public static JButton button1=new JButton("监听声音");

public static JButton button2=new JButton("停止");

public static JButton button3=new JButton("退出");

//public static JButton button4=new JButton("播放");

//public static JButton button5=new JButton("绘图");

//定义一个面板来放置按钮

JPanel pane2=new JPanel();

//定义一个面板用于绘制波形

panel jpanel=new panel();

//实例化监听类

public static MyActionListenerbutton1 bt1=new MyActionListenerbutton1();

public static MyActionListenerbutton2 bt2=new MyActionListenerbutton2();

public static MyActionListenerbutton3 bt3=new MyActionListenerbutton3();

//public static MyActionListenerbutton4 bt4=new MyActionListenerbutton4();

//public static MyActionListenerbutton5 bt5=new MyActionListenerbutton5();

//控制录音标志

public static boolean stopCapture = false;

//采集数据是否准备好

public static boolean dataready=true;

//录音格式

public static AudioFormat audioFormat;

//读取数据:从TargetDataLine写入ByteArrayOutputStream录音

public static ByteArrayOutputStream byteArrayOutputStream;

public static int totaldatasize = 0;

   public static TargetDataLine targetDataLine;

   //录音缓冲数组

   public static byte[] tempBuffer=new byte[1024];

   //绘图缓冲区

   public static byte[] draw=new byte[1024];

   //播放数据:从AudioInputStream写入SourceDataLine播放

   //public static AudioInputStream audioInputStream;

   //public static SourceDataLine sourceDataLine;

  /** private byte[] audioDatas = null;

private int intBytes = 0;

private byte[] ml = new byte[1];

private int[] drawl = null;**/

   static int[] drawl = null;

   private int intBytes = 0;

   //static byte [] bs=null;


public audio(){

//设置标题

this.setTitle("说话人识别");

//设置窗体的大小

this.setSize(600,600);

//设置窗体的位置

this.setLocation((width-600)/2,(height-600)/2);

//设置窗体的大小不可调整

this.setResizable(false);

//设置退出方式

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//绝对布局

setLayout(null);

//设置按钮面板位置

pane2.setBounds(10,10,560,60);

pane2.setBorder(BorderFactory.createLineBorder(Color.red));

//加入按钮面板

this.getContentPane().add(pane2);

pane2.setLayout(null);

//设置按钮的位置

 button1.setBounds(10,10,90,40);

button2.setBounds(110,10,90,40);

button3.setBounds(210,10,90,40);

// pane2.add(button4);

//pane2.add(button5);

 //将按钮添加的按钮面板上

pane2.add(button1);

pane2.add(button2);

pane2.add(button3);

//设置绘图面板的位置及大小

jpanel.setBounds(10,100,560,200);

//设置绘图面板的颜色

jpanel.setBorder(BorderFactory.createLineBorder(Color.red));

//加入绘制波形的面板

this.getContentPane().add(jpanel);

//注册监听

button1.addActionListener(bt1);//监听

button2.addActionListener(bt2);//停止

button3.addActionListener(bt3);//退出

//button4.addActionListener(bt4);//回放

//button5.addActionListener(bt5);//绘图

//设置状态

button1.setEnabled(true);//监听

button2.setEnabled(false);//停止

button3.setEnabled(true);//退出

//button4.setEnabled(false);//回放

//button5.setEnabled(false);

//绘图

repaint();

      //设置窗体为可见

this.setVisible(true);

}


//定义开始监听类按钮程序

public static class MyActionListenerbutton1 implements ActionListener{

public void actionPerformed(ActionEvent e){

button1.setEnabled(false);

button2.setEnabled(true);

button3.setEnabled(true);

//button4.setEnabled(false);

//button5.setEnabled(true);

capture();//开始录音  

}

}

//定义停止类监听程序

public static class MyActionListenerbutton2 implements ActionListener{

public void actionPerformed(ActionEvent e){

button1.setEnabled(true);

button2.setEnabled(false);

button3.setEnabled(true);

//button4.setEnabled(true);

//button5.setEnabled(true);

stop();//停止

}

}

//定义退出类监听程序

public static class MyActionListenerbutton3 implements ActionListener{

public void actionPerformed(ActionEvent e){

   button1.setEnabled(false);

button2.setEnabled(true);

button3.setEnabled(true);

//button4.setEnabled(true);

//button5.setEnabled(true);

exit();//退出

}

}

//定义回放类监听程序

/**public static class MyActionListenerbutton4 implements ActionListener{

public void actionPerformed(ActionEvent e){

play();//播放录音

}

}**/

//录音

public static void capture() {

// TODO Auto-generated method stub

 //打开录音

  try {

  //获得录音格式

  audioFormat = getAudioFormat();

  //取得输入设备信息

  DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);

  //取得输入设备

  targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);

  //打开输入设备

  targetDataLine.open(audioFormat);

  //开始录音

  targetDataLine.start();

  //创建独立线程进行录音

  Thread captureThread = new Thread(new CaptureThread());

  captureThread.start();

} catch (LineUnavailableException e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.exit(0);

}

}

//播放类

/**private static void play() {

// TODO Auto-generated method stub

try

 {

  //取得录音数据

  byte audioData[] = byteArrayOutputStream.toByteArray();

  //转换成输入流

  InputStream byteArrayInputStream = new ByteArrayInputStream(audioData);

  AudioFormat audioFormat = getAudioFormat();

  audioInputStream = new AudioInputStream(byteArrayInputStream,audioFormat, audioData.length / audioFormat.getFrameSize());

  DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);

  sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);

  sourceDataLine.open(audioFormat);

  sourceDataLine.start();

  //创建独立线程进行播放

  Thread playThread = new Thread(new PlayThread());

  playThread.start();

   }

 catch(Exception e)

 {

  e.printStackTrace();

  System.exit(0);

 }

}**/

//暂停

private static void stop() {

// TODO Auto-generated method stub

stopCapture = true;

}

//退出

private static  void exit() {

System.exit(0);

}

//获取AudioFormat

private static AudioFormat getAudioFormat()

{

 float sampleRate = 16000.0F;//8000,11025,16000,22050,44100

 int sampleSizeInBits = 16;//8,16

 int channels = 1;//1,2

 boolean signed = true;//true,false

 boolean bigEndian = false;//true,false

 return new AudioFormat(sampleRate,sampleSizeInBits,channels,signed,bigEndian);

}

//录音线程

private static class CaptureThread extends Thread

{

//读取录音数据,数据存放在tempbuffer数组中

 byte tempBuffer[]=new byte[1024];

 public void run()

 {

  byteArrayOutputStream = new ByteArrayOutputStream();

  totaldatasize = 0;

  stopCapture = false;

  try

  {

   while(!stopCapture)

   {

    //读取1024个数据

    int cnt = targetDataLine.read(tempBuffer,0,tempBuffer.length);

    dataready=true;

  //将采集到的数据存数绘图数组

System.arraycopy(tempBuffer, 0, draw, 0, draw.length);

    if(cnt>0)

    {

     //保存该数据

     byteArrayOutputStream.write(tempBuffer, 0, cnt);

     totaldatasize += cnt;

      /**输出tempBuffer数组中的数据

     for(int j=0;j<=tempBuffer.length;j++){

 System.out.println(tempBuffer[j]);

     }**/

    }

   }

   //将缓冲数组中的数据和大小,传送到数组bs中

        //   byte [] bs= byteArrayOutputStream.toByteArray();

         /**  for(int j=0;j<=bs.length;j++){

System.out.println(bs[j]);

           }**/

          byteArrayOutputStream.close();

  }

  catch(Exception e)

  {

   e.printStackTrace();

   System.exit(0);

  }

 }

}

//绘图线程

/**static  class drawthread extends Thread{

public void run(){

while(true){

if(dataready){

   //将采集到的数据存数绘图数组

//System.arraycopy(tempBuffer, 0, draw, 0, draw.length);

//数据已经取走

dataready=false;

}

}

}

}**/

//播放线程

/**private static class PlayThread extends Thread//播放线程

{

 byte tempBuffer[] = new byte[1024];

 public void run()

 {

  try

  {

   int cnt;

   //读取数据到缓存数据

   while ((cnt=audioInputStream.read(tempBuffer,0,tempBuffer.length))!=-1)

   {

    if(cnt>0)

    {

     //写入缓存数据

     sourceDataLine.write(tempBuffer, 0, cnt);

    }

   }

   //Block等待临时数据被输出为空

   sourceDataLine.drain();

   sourceDataLine.close();

  }

  catch(Exception e)

  {

   e.printStackTrace();

   System.exit(0);

  }

 }

}**/

/**public void paint(Graphics g){

for(int i=0;i<bs.length;i++){

drawl[i]=Math.abs((int) bs[0]);

}

for(int i=0;i<drawl.length;i++){

g.drawLine(i+100, drawl[i],(i+1)+100,drawl[i+1]);

}

}**/

//jpanel画图的类

class panel extends JPanel  {

public void paintComponent(Graphics g){

/**while(true){

if(dataready){

   //将采集到的数据存数绘图数组

//System.arraycopy(tempBuffer, 0, draw, 0, draw.length);

//数据已经取走

dataready=false;

}

}**/

   dataready=false;

for(int i=0;i<draw.length;i++){

drawl[i]=Math.abs((int) draw[0]);

}

for(int i=0;i<drawl.length;i++){

g.drawLine(i+20,drawl[i],(i+1)+20,draw[i+1]);

}

/**for(int i=0;i<bs.length;i++){

drawl[i]=Math.abs((int) bs[0]);

}

for(int i=0;i<drawl.length;i++){

g.drawLine(i+30, drawl[i],(i+1)+30,drawl[i+1]);

}**/

}

}

}

这个程序 您帮我瞅瞅 哪里有问题。我定义了两个jpanel面板,能够正常显示,但是在paint中绘制动态的波形图时不能正常显示两个面板,而且波形还不能显示。这个程序是实现一边录音,一边绘制绘制出波形图。


你可能感兴趣的:(java录音,绘制波形程序)