Hello! I'm currently working on a project for my java class. I have it almost completely done, but the last thing I need to do is program 2 jbuttons to turn a radio on and off. I have it coded now where it will start playing the URL I've linked it to, and the code below compiles with the rest of my program, but I'm not sure how to alter it to make it so that when I click it again, it turns off. I tried writing a for loop, but the way I was going about it was clearly not working...I was trying to create the sound file "ac" in the try catch, and then use an if then with ac.loop() and ac.stop(), based on a number being 1 or -1, but my program didn't seem to like that.
Any suggestions?
jbtRadio1.addActionListener(new ActionListener() { //Programs Get Angry Car 1 to play a sound public void actionPerformed(ActionEvent e) { try { URL url = new URL("http://www.angelfire.com/sc/wavsetc8/images/2sexy.wav"); AudioClip ac = Applet.newAudioClip(url); ac.loop(); } catch (Exception f) { System.out.println(f); } }}); jbtRadio2.addActionListener(new ActionListener() { //Programs Get Angry Car 2 to play a sound public void actionPerformed(ActionEvent e) { try { URL url = new URL("http://www.angelfire.com/sc/wavsetc2/images/500miles.wav"); AudioClip ac = Applet.newAudioClip(url); ac.loop(); } catch (Exception f) { System.out.println(f); } }});