[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Can't 'loop' an audio clip properly

Posted on 2005-04-17
11
Medium Priority
?
274 Views
Last Modified: 2010-05-18
At the moment using this:

  //Start the applet

 private AudioClip clip = null;
    private boolean looping = false;

    public void start() {

        try {
            clip =
                    java.applet.Applet.newAudioClip(new java.net.URL(
                    getDocumentBase(), "1000Hz.au"));
        } catch (MalformedURLException ex) {
        }

      clip.play( );

    }


    //Stop the applet
    public void stop() {
         if (looping) {
            clip.stop();    //Stop the sound loop.
         }
 }

just playes the sound once. Any changes I try and make results in the sound looping and never stopping.
0
Comment
Question by:pigmentarts
  • 5
  • 5
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13800688
Try using an inner class to wrap a separate thread:

ClipPlayer player = new ClipPlayer(clip);
new Thread(player).start();

// when you want to stop it...
player.setShouldStop(true);

.........

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();
            }
      }
}
0
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 800 total points
ID: 13800747
Hi pigmentarts,


try using loop() instead of play()

   public void start() {

        try {
            clip =
                    java.applet.Applet.newAudioClip(new java.net.URL(
                    getDocumentBase(), "1000Hz.au"));

            clip.loop( );

        } catch (MalformedURLException ex) {
        }
    }


    //Stop the applet
    public void stop() {
        // stop it anyway.
        if (clip!=null) clip.stop();    //Stop the sound loop.
    }
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 13800903
CEHJ could you show me how i should implement

ClipPlayer player = new ClipPlayer(clip);
new Thread(player).start();

in my code please, hopefully It will teach me how to use classes within classes.



Webstorm, you code works great when running as an applet. But I still have a probelm stopping the sound in the java browser. I want it to stop when i click off the page.
0
Industry Leaders: 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 12

Author Comment

by:pigmentarts
ID: 13800914
Here is the entire code

package jas;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;




public class PlaySound extends Applet {
    boolean isStandalone = false;
    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 {
    }

    //Start the applet

 private AudioClip clip = null;
    private boolean looping = false;

    public void start() {

         try {
             clip =
                     java.applet.Applet.newAudioClip(new java.net.URL(
                     getDocumentBase(), "1000Hz.au"));

             clip.loop( );

         } catch (MalformedURLException ex) {
         }
     }


     //Stop the applet
     public void stop() {
         // stop it anyway.
         if (clip!=null) clip.stop();    //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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13801029
Paste this in to your code:


boolean isStandalone = false;
private ClipPlayer player;
private AudioClip clip;


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


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



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();
          }
     }
}
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 13801107
Sorry about this, I'm getting myself lost. I just tried making a new class 'ClipPlayer' to store the method of playing the clip...but got in a muddle. So i've left it as a nothing class.

I think this is how I should have pasted it, but now i don't get any sound playing at all :(


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 {
     player = new ClipPlayer(clip);
     new Thread(player).start();
     try {
          clip = java.applet.Applet.newAudioClip(new java.net.URL(getDocumentBase(), "1000Hz.au"));
     }
     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.
}
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1200 total points
ID: 13801118
You have your initialization the wrong way around. Try

//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();
      }
}
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 13801180
I think i need to give you a 1000 points for the time spent on this question..

Now i run the app, i hear the sound once, then the app loads and plays the sound again.

Thats all i can do. Clicking on Start and stop does nothing after this.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13801216
>>Clicking on Start and stop does nothing after this.

Where is the code that receives your clicks?
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 13801328
I thought that was done in  //Start the applet and //Stop the applet

Before i started to loop the sound, I could click on start to play it once, then stop. Start would play it again
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13801824
I think you may have been using some sort of IDE, otherwise i can't imagine what you're clicking on. Try moving this to the applet's start method:

new Thread(player).start();
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month18 days, 15 hours left to enroll

834 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