title: Java - How to stop an audioclip from another class
body: <p>Right now, i'm making an app that involves background audio, and with a couple of tips from other's questions and answers, i was able to make the audio work. </p>
  
  <p>But right now, i'm trying to use a button in a  different class to stop an audioclip, that was instantiated in another class.</p>
  
  <p>When i tested the prototype, all in the same class, it worked perfectly. </p>
  
  <p>But in my main app, the "stop" button is in a different class (for good reason). I don't know if you'll want my code for the main app, but here's the prototype that i described: Any help would be appreciated, thanks.</p>
  
  <pre><code>public class AudioPlay  {
  
      Clip clip;
  
     // Constructor
     public AudioPlay() {
          try {
              this.clip = AudioSystem.getClip();
          } catch (LineUnavailableException ex) {
              Logger.getLogger(AudioPlay.class.getName()).log(Level.SEVERE, null, ex);
          }
  
         JFrame f = new JFrame();
  
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setTitle("Test Sound Clip");
        f.setSize(300, 200);
        f.setLayout(new FlowLayout());
  
        JButton button = new JButton("play");
          button.addActionListener(new  ActionListener() {
             public void actionPerformed(ActionEvent e) {                    
               playSound(0);                                     
             }
          });            
  
          JButton button2 = new JButton("stop");
          button2.addActionListener(new  ActionListener() {
             public void actionPerformed(ActionEvent e) {
                  clip.stop();                          
             }
          });
  
          f.add(button);
          f.add(button2);
          f.setVisible(true);
  
     }
  
     public void playSound(int a){
  
         // Open an audio input stream.
         String[] sounds = new String[10];
         sounds[0]= "/audioplay/sounds/kk.wav";
         sounds[1]= "/audioplay/sounds/btn2.wav";
         try {
           URL url = this.getClass().getResource(sounds[a]);
           AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
  
           // Open audio clip and load samples from the audio input stream.
           clip.open(audioIn);
           clip.start();            
  
        } catch (UnsupportedAudioFileException e) {
           e.printStackTrace();
        } catch (IOException e) {
           e.printStackTrace();
        } catch (LineUnavailableException e) {
           e.printStackTrace();
        }
  
  }
  
     public static void main(String[] args) {
         new AudioPlay();
     }
  }
  </code></pre>
  
  <p>EDIT
  here's the actual application
  first of is the main class.............</p>
  
  <pre><code>public class Operator {
       static boolean player = false;
       Clip clip;
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args) {
  
          // TODO code application logic here
          Welcome on = new Welcome();
          on.setLocationRelativeTo(null);
          on.setVisible(true);
  
  
      }
  
      public Operator() {
           try {
               this.clip = AudioSystem.getClip();
           } catch (LineUnavailableException ex) {
               Logger.getLogger(Operator.class.getName()).log(Level.SEVERE, null, ex);
           }
  
      }
  
  
       public void playBackground(int a){
      String[] sounds = new String[10];
         sounds[0]= "/operator/sounds/kk.wav";
         sounds[1]= "/operator/sounds/mnt5.wav";
  
        try {
           // Open an audio input stream.
            URL url= this.getClass().getResource(sounds[a]);
  
           AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
           // Get a sound clip resource.
  
           // Open audio clip and load samples from the audio input stream.
           clip.open(audioIn);
           clip.start();
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
        }
      }
  
      }
  </code></pre>
  
  <p>now next up, is where the sound begins to play</p>
  
  <pre><code>public class MainMenuTest extends javax.swing.JPanel {
  
     // this is for referencing 
      Operator sound = new Operator();
       static int course;
       static String courseName;
  
      //constructor  obviously..........
      public MainMenuTest() {
  
          initComponents();
      }
  
  
  
  
  
      public javax.swing.JPanel jPanel1;
  
  
      @SuppressWarnings("unchecked")
  
      private void initComponents() {
  
          jPanel1 = new javax.swing.JPanel();
  
  
  
          sound.playBackground(0);
      /* writing this stop() action here below, causes the audio not to play, which is actually 
          what i want, but in another class 
          sound.clip.stop();
  */
          //most of the components are removed, they weren't really relevant
  
  
      }                                      
  
  
  }
  </code></pre>
  
  <p>and lastly, the class in which the clip is stopped</p>
  
  <pre><code>public final class Options extends JPanel {
      // referencing stuff
      Operator sound = new Operator();
       private JButton Start;
  
  
      public Options() {
          initComponents();
  
      }
  
  
      @SuppressWarnings("unchecked")
  
      private void initComponents() {
  
  
          Stop = new javax.swing.JButton("stop");
  
  
  
       Stop.addActionListener(new  ActionListener() {
        public void stopActionPerformed(ActionEvent e) {
             //this looks like the same stop() method  wrote before, but here, it ain't working
              sound.clip.stop();      
  
  
      }});                                    
  
      }                                           
  
  
  
  
  
  }
  </code></pre>