Results 1 to 3 of 3
- 10-13-2012, 05:58 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 88
- Rep Power
- 0
GUI - Button to start, button to stop
Hello. I have written a very simple program (using NetBeans) that presents the users with a GUI with two buttons on it. Click on one button, and the program plays back an audio file (looped 3x) for the users' listening entertainment. Push the other button, and the program plays back a different audio file (also looped 3x).
The code is below, and everything works. Here is my question: because the bit of audio is looped, playback time goes on for approx 20 seconds, and I would like to give the user the option of stopping the playback. I know I can add a button to stop audio playback. But I am wondering if there is a way so the user can click once on a button to start playback, and then click a second time on the same button to stop audio playback.
So, is this possible (different actions resulting from sequential clicks on the same button), and can anyone drect me on how to accomplish this?
Thank you and here is the code so far (again, this is NetBeans and I have omitted some of the code).
Java Code:import java.io.*; import java.net.URL; import javax.sound.sampled.*; import javax.swing.*; public class sample_form extends javax.swing.JFrame { public sample_form() { initComponents(); } private void butSamp1ActionPerformed(java.awt.event.ActionEvent evt) { try { // Open an audio input stream File sound1 = new File("C:/users/jmp/Desktop/Java 6/contemp76.wav"); AudioInputStream audioIn = AudioSystem.getAudioInputStream(sound1); // Get a sound clip resource Clip clip = AudioSystem.getClip(); // Open audio clip and load sample from the audio input stream clip.open(audioIn); clip.loop(3); } catch (UnsupportedAudioFileException uae) { System.out.println(uae.getMessage()); } catch (IOException ioe) { System.out.println(ioe.getMessage()); } catch (LineUnavailableException lue) { lue.printStackTrace(); } } private void butSamp2ActionPerformed(java.awt.event.ActionEvent evt) { try { // Open an audio input stream File sound2 = new File("C:/users/jmp/Desktop/Java 6/contemp88.wav"); AudioInputStream audioIn = AudioSystem.getAudioInputStream(sound2); // Get a sound clip resource Clip clip = AudioSystem.getClip(); // Open audio clip and load sample from the audio input stream clip.open(audioIn); clip.loop(3); } catch (UnsupportedAudioFileException uae) { System.out.println(uae.getMessage()); } catch (IOException ioe) { System.out.println(ioe.getMessage()); } catch (LineUnavailableException lue) { lue.printStackTrace(); } }
- 10-13-2012, 06:53 PM #2
Re: GUI - Button to start, button to stop
Use a boolean to check which action should be performed. Switch the boolean when you perform an action.
Also, most people here would recommend against using a gui builder until you really understand what's going on behind the scenes.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 10-13-2012, 07:04 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 88
- Rep Power
- 0
Re: GUI - Button to start, button to stop
Hello. Thank you kevin for your response.
You are of course correct: my question is about my (lack of) understanding of Swing. The code that plays back the audio file functions just fine.
Believe it or not, I first wrote the playback part of the code in Wordpad, and ran it from the command prompt. It works so then I wanted to add a bit of a GUI.
And believe it or not: I am working my way through some tutorials about Swing, so I'm working on improving my understanding of Swing.
I will see what I can do with the boolean idea. Thanks again.
Similar Threads
-
J2ME, stop play after pressed button
By derata in forum CLDC and MIDPReplies: 3Last Post: 04-20-2012, 12:00 PM -
START/STOP Button
By tnrh1 in forum New To JavaReplies: 13Last Post: 08-18-2011, 02:40 PM -
Stopping a thread using a stop button - GUI
By Ben1 in forum New To JavaReplies: 5Last Post: 01-27-2011, 04:21 PM -
Use stop button to stop moving (stop timers) on JPanel
By mneskovic in forum New To JavaReplies: 3Last Post: 07-23-2010, 12:50 PM -
stop button in the browser
By Peter in forum Java ServletReplies: 2Last Post: 07-04-2007, 07:21 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks