[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Coding the stop and start buttons for an application

Posted on 2005-04-17
6
Medium Priority
?
190 Views
Last Modified: 2010-03-31
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
Comment
Question by:pigmentarts
  • 4
  • 2
6 Comments
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13801589
Call this:

clip.loop();
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13801591
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
 
LVL 12

Author Comment

by:pigmentarts
ID: 13801646
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Accepted Solution

by:
InteractiveMind earned 2000 total points
ID: 13801661
Perhaps try something like this:


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



?
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 13801677
Ah thats great yes..i'll close this one and open another question about getting it to stop in the browser
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13801721
Thanks :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month17 days, 15 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question