-2

I've been trying to create a functioning mute button in swing. On start up a clip plays in my application. There is a button that (is supposed) to allow the user to stop the music. What am I doing wrong?

This is the code from my main method.

try {
    File file = new File("Sounds/MenuMusic.wav"); // src is the location of the file
    AudioInputStream stream = AudioSystem.getAudioInputStream(file);
    Clip clip = AudioSystem.getClip();
    clip.open(stream);
    clip.start();
    if(mute==true){
        clip.stop();
    }
} catch (Exception e) {
    e.printStackTrace();
}

And this is the action listener on my mute button.

btnSound.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        mute=true;
    }
});
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
NemoLKLK
  • 35
  • 5

1 Answers1

2
private Clip clip; 

private myMethod(){
       try {
            File file = new File("Sounds/MenuMusic.wav"); // src is the location of the file
            AudioInputStream stream = AudioSystem.getAudioInputStream(file);
            clip = AudioSystem.getClip();
            clip.open(stream);
            clip.start();

        } catch (Exception e) {
            e.printStackTrace();
        }
}

btnSound.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            clip.stop();
                }
            });

Im just assuming everything is in the same class.

user2891133
  • 309
  • 2
  • 4
  • 13