Java Clip赢得';当我按下按钮时,不要停止并继续播放.wav文件

Java Clip赢得';当我按下按钮时,不要停止并继续播放.wav文件,java,eclipse,audio,wav,clip,Java,Eclipse,Audio,Wav,Clip,我尝试制作一个程序来播放和停止wav,当我按下播放按钮时,代码可以播放wav,但当我按下停止按钮时,代码无法停止,当我再次按下播放按钮时,它会再次播放声音,但之前会合并声音,代码如下: public constructor(){ btnPlaySound.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { try {

我尝试制作一个程序来播放和停止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();

    }
}

下面是我对您尝试执行的操作的一个小实现-请务必注意,剪辑是全局的,以便您可以访问它,并且操作已移动到按钮:

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();
  }

}

如果你把整个节目都放上,我们也许能看到什么happens@gpasch在那里我编辑了我的代码