Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default 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();
    	}      
        }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default 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!

  3. #3
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default 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

  1. J2ME, stop play after pressed button
    By derata in forum CLDC and MIDP
    Replies: 3
    Last Post: 04-20-2012, 12:00 PM
  2. START/STOP Button
    By tnrh1 in forum New To Java
    Replies: 13
    Last Post: 08-18-2011, 02:40 PM
  3. Stopping a thread using a stop button - GUI
    By Ben1 in forum New To Java
    Replies: 5
    Last Post: 01-27-2011, 04:21 PM
  4. Use stop button to stop moving (stop timers) on JPanel
    By mneskovic in forum New To Java
    Replies: 3
    Last Post: 07-23-2010, 12:50 PM
  5. stop button in the browser
    By Peter in forum Java Servlet
    Replies: 2
    Last Post: 07-04-2007, 07:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •