title: disable other sounds in java
body: <p>I wrote a program in Java using the pi4j lib to make sound whenever a (physical) button is clicked. This program works, but it now plays all the sounds interchangeably. I want that when you click on 2,3,4 or more buttons you only hear one sound. </p>
  
  <p>This is the code I hope you can help. </p>
  
  <pre><code>public class ButtonSoundsProject{
  public static void main(String args[]) throws InterruptedException {
      System.out.println("Toy has been started!");
  
      // create gpio controller
      final GpioController gpio = GpioFactory.getInstance();
  
      // provision gpio pin #02 as an input pin with its internal pull down resistor enabled
      GpioPinDigitalInput[] pins = {
          gpio.provisionDigitalInputPin(RaspiPin.GPIO_00, PinPullResistance.PULL_DOWN),
          gpio.provisionDigitalInputPin(RaspiPin.GPIO_01, PinPullResistance.PULL_DOWN),
          gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, PinPullResistance.PULL_DOWN),
          gpio.provisionDigitalInputPin(RaspiPin.GPIO_03, PinPullResistance.PULL_DOWN),
          gpio.provisionDigitalInputPin(RaspiPin.GPIO_04, PinPullResistance.PULL_DOWN),
          gpio.provisionDigitalInputPin(RaspiPin.GPIO_05, PinPullResistance.PULL_DOWN),};
  
      final ArrayList&lt;String&gt; soundList = new ArrayList&lt;String&gt;();
      soundList.add("/home/pi/Sounds/Sound1.wav");
      soundList.add("/home/pi/Sounds/Sound2.wav");
      soundList.add("/home/pi/Sounds/Sound3.wav");
      soundList.add("/home/pi/Sounds/Sound4.wav");
      soundList.add("/home/pi/Sounds/Sound5.wav");
      soundList.add("/home/pi/Sounds/Sound6.wav");
      soundList.add("/home/pi/Sounds/Sound7.wav");
      soundList.add("/home/pi/Sounds/Sound8.wav");
      soundList.add("/home/pi/Sounds/Sound9.wav");
      soundList.add("/home/pi/Sounds/Sound10.wav");
      soundList.add("/home/pi/Sounds/Sound11.wav");
      soundList.add("/home/pi/Sounds/Sound12.wav");
  
  
      // create and register gpio pin listener
      GpioPinListenerDigital listener = new GpioPinListenerDigital() {
          @Override
          public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
              // display pin state on console
  
              final int randomNum = 0 + (int) (Math.random() * 12);
              System.out.println(randomNum);
  
              System.out.println(" --&gt; GPIO PIN STATE CHANGE: " + event.getPin() + " = " + event.getState());
              InputStream in;
              try {
                  System.out.println(soundList.get(randomNum).toString());
                  String filepath = soundList.get(randomNum).toString();
                  in = new FileInputStream(new File(filepath));
                  AudioStream as = new AudioStream(in);
                  AudioPlayer.player.start(as);
  
              } catch (Exception ex) {
                  ex.printStackTrace();
              }
          }
  
      };
  
      gpio.addListener(listener, pins);
  
  
      for (;;) {
          Thread.sleep(500);
      }
  }
  
  }    
  </code></pre>