Search code examples
javaeclipseaudiowavclip

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


I try to make a program to play and stop wav, the code can play wav when i hit the play button but can't stop it when i hit a stop button, and when i hit the play button again it play the sound again but merging the sound before, here's the code :

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

    }
}

Solution

  • Here is my little implementation of what you are trying to do - it is important to note that the clip is made global so that you have access to it, and the action has been moved to the button:

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