Question

I have inserted a background audio into my program using AudioInputStream.

When I press the On, the background audio will play, and Off will stop playing the background audio.

Here is the part of my code for the audio playing part :

public void playSound(int i)
{
    try 
    {
        .  //Others buttons sound
        .  //Others buttons sound
    if(i == 3 || i == 4)
    {
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("Background.wav"));
        Clip clip3 = AudioSystem.getClip( );
        clip3.open(audioInputStream);
        if(i == 3)
        {
            clip3.loop(Clip.LOOP_CONTINUOUSLY);
            clip3.start();
            settingSubMenuItem3.setEnabled(false);
            settingSubMenuItem4.setEnabled(true);
        }
        if(i == 4)
        {
            clip3.stop();
            settingSubMenuItem3.setEnabled(true);
            settingSubMenuItem4.setEnabled(false);
        }
    }
    catch(Exception e)
    {
        System.out.println("Error with playing sound.");
        e.printStackTrace();
    }
}

And here is the ButtonListener part :

private class MenuItemListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == settingSubMenuItem3)
        {
            playSound(3);
        }
        if(e.getSource() == settingSubMenuItem4)
        {
            playSound(4);       
        }
    }
}

My code can play the audio when I press On button, but when I press Off button, it does not work.

There is no error during compile and run.

What is the error of my code?

Was it helpful?

Solution

Your AudioInputStream variable, audioInputStream, and Clip variable, clip3, are local to the method. The object you are trying to stop is not the same as the one that's playing currently.

Make them class fields, check that they're not null before calling methods on them, don't create a new object if you're trying to stop the currently playing object, and you should be OK.

Something like:

public void playSound(int i) {
  try {
     // ...
     if (i == 3 || i == 4) {
        if (i == 3) {
           audioInputStream = AudioSystem
                 .getAudioInputStream(new File("Background.wav"));
           clip3 = AudioSystem.getClip();
           clip3.open(audioInputStream);
           clip3.loop(Clip.LOOP_CONTINUOUSLY);
           clip3.start();
           settingSubMenuItem3.setEnabled(false);
           settingSubMenuItem4.setEnabled(true);
        }
        if (i == 4) {
           if (clip3 != null && clip3.isActive()) {
              clip3.stop();
              settingSubMenuItem3.setEnabled(true);
              settingSubMenuItem4.setEnabled(false);
           }
        }
     }
  } catch (Exception e) {
     System.out.println("Error with playing sound.");
     e.printStackTrace();
  }
}

Again, making audioInputStream and clip3 non-static class fields.

As an aside, I would avoid using "magic" numbers such as 3 and 4 as you are doing as this may become a devil to debug 6 months from now. Instead give each JButton its own Action. It deserves no less.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top