Java 当我按下按钮时剪辑不会停止并继续播放 .wav 文件

Java Clip won't stop and keep playing .wav file when i hit the button

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

}