• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

Coding the stop and start buttons for an application

I have been building an applet to play a sound file when the app is run or the java browser is run.

The buttons seemed to work fine when playing the sound, but now i'm trying to loop the sound until the stop button is pressed.

What i would like is for the sound to be looped when the html page is opened, and then stops playing when a user clicks to another page.


Here is the code i'm using so far:

public class PlaySound extends Applet {
    boolean isStandalone = false;
 private ClipPlayer player;
 private AudioClip clip;
 

    BorderLayout borderLayout1 = new BorderLayout();

    //Get a parameter value
    public String getParameter(String key, String def) {
        return isStandalone ? System.getProperty(key, def) :
                (getParameter(key) != null ? getParameter(key) : def);
    }

    public static void main(String[  ] args)
       throws java.net.MalformedURLException
   {

    }

    //Construct the applet
    public PlaySound() {
    }

    //Initialize the applet
    public void init() {
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //Component initialization
private void jbInit() throws Exception {
     try {
          clip = java.applet.Applet.newAudioClip(new java.net.URL(getDocumentBase(), "1000Hz.au"));
          player = new ClipPlayer(clip);
          new Thread(player).start();
     }
     catch(Exception e) {
          e.printStackTrace();
     }
}



    //Start the applet

    class ClipPlayer implements Runnable {
        private AudioClip clip;
        private boolean shouldStop;
   
        public ClipPlayer(AudioClip clip) {
             this.clip = clip;
        }
   
        public void setShouldStop(boolean value) {
             shouldStop = value;
        }
   
        public void run() {
             while(!shouldStop) {
                  clip.play();
             }
        }
   }



   //Stop the applet
public void stop() {
   // stop it anyway.
   player.setShouldStop(true);    //Stop the sound loop.
}



    //Destroy the applet
    public void destroy() {
    }

    //Get Applet information
    public String getAppletInfo() {
        return "Applet Information";
    }

    //Get parameter info
    public String[][] getParameterInfo() {
        return null;
    }
}
0
pigmentarts
Asked:
pigmentarts
  • 4
  • 2
1 Solution
 
InteractiveMindCommented:
Call this:

clip.loop();
0
 
InteractiveMindCommented:
You should do it here..

Replace:

        public ClipPlayer(AudioClip clip) {
             this.clip = clip;
        }

with:

        public ClipPlayer(AudioClip clip) {
             this.clip = clip;
             this.clip.loop();
        }
0
 
pigmentartsAuthor Commented:
This does loop the sound, but I can't stop it. Pressing the stop button does nothing.

Its the same for the browser, the sound keeps playing and won't stop when i leave the page.

This code below seems to be what i want but i'm too confused with my code. Can you make the loop stop!! :P

public void stop() {
        if (looping) {
        loopClip.stop();    //Stop the sound loop.
    }
}
 
public void start() {
    if (looping) {
        loopClip.loop();    //Restart the sound loop.
    }
}    

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
InteractiveMindCommented:
Perhaps try something like this:


public void run() {
     while(!shouldStop) {
          clip.play();
     }
     clip.stop();
}



?
0
 
pigmentartsAuthor Commented:
Ah thats great yes..i'll close this one and open another question about getting it to stop in the browser
0
 
InteractiveMindCommented:
Thanks :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now