当按下按钮

时间:2016-03-10 15:38:09

标签: java eclipse audio wav clip

我尝试制作一个程序来播放并停止wav,当我按下播放按钮时代码可以播放wav但是当我按下停止按钮时无法停止它,当我再次点击播放按钮时它会播放再次响起,但之前合并声音,这是代码:

public constructor(){
btnPlaySound.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    btnPlaySoundCLick();
                } catch (LineUnavailableException | IOException
                        | UnsupportedAudioFileException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });}

 private void btnPlaySoundCLick() throws LineUnavailableException, IOException, UnsupportedAudioFileException{ 

    File soundFile = new File(path);
    AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);

    DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
    Clip clip = (Clip) AudioSystem.getLine(info);
    clip.open(sound);

    clip.addLineListener(new LineListener() {
      public void update(LineEvent event) {
        if (event.getType() == LineEvent.Type.STOP) {
          event.getLine().close();
          clip.stop();
        }
      }
    });

    // play the sound clip
    if(btnPlaySound.getText().equals("Listen")){
        btnPlaySound.setText("Stop");
        clip.start();
    } else if(btnPlaySound.getText().equals("Stop")) {
        btnPlaySound.setText("Listen");
        clip.stop();

    }
}

1 个答案:

答案 0 :(得分:0)

这是我对你要做的事情的一点实现 - 重要的是要注意剪辑是全局的,以便你可以访问它,并且动作已经移动到按钮:

public class c extends JFrame {

  String f="e-22.wav";
  JButton btnPlaySound=new JButton("Listen");
  Clip clip = null;

public c(){

  btnPlaySound.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
              JButton b=(JButton)arg0.getSource();
              // play the sound clip
              if(b.getText().equals("Listen")){
                b.setText("Stop");
                btnPlaySoundCLick();
              } else if(btnPlaySound.getText().equals("Stop")) {
                  b.setText("Listen");
                  clip.stop();
              }
            } catch (LineUnavailableException | IOException
                    | UnsupportedAudioFileException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

  setSize(500, 500);
  add(btnPlaySound);
  setVisible(true);

}

 private void btnPlaySoundCLick() throws LineUnavailableException, IOException, UnsupportedAudioFileException{ 

  File soundFile = new File(f);
  AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);

  DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
  clip = (Clip) AudioSystem.getLine(info);
  clip.open(sound);

  clip.addLineListener(new LineListener() {
  public void update(LineEvent event) {
    if (event.getType() == LineEvent.Type.STOP) {
    System.out.println("stop");
      event.getLine().close();
    }
  }
  });

  clip.start();

  }


  public static void main(String[] args) {
    c cc=new c();
  }

}