Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

Stopping a sound from looping in the browser

I can run an application which loops a sound until the stop button is pressed.

But how do I make the sound stop looping in the browser?

// Ideas i have

1. Create a button in design mode, but have no idea how to attach it to my current code.

2. To stop the sound playing when a leaving the page
0
pigmentarts
Asked:
pigmentarts
  • 15
  • 11
  • 2
2 Solutions
 
InteractiveMindCommented:
override the destroy() method of the applet, like so:

   public void destroy()
   {
      clip.stop();
   }

0
 
InteractiveMindCommented:
Also, add the following method to the ClipPlayer class that you've created:

   public void stopPlay()
   {
      clip.stop();
      this.shouldStop = true;
   }

Then in your applets stop() method, use this:

   public void stop()
   {
      player.stopPlay();
   }


I'm not sure why your code isn't working already, but see if this helps :0
0
 
pigmentartsAuthor Commented:
No it still runs the same.

Maybe its because i'm running 2 classes in the same java file.  Here is my code, it must be something i've got in there by mistake or its because i've got 2 classes in there...What do you think?

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(getCodeBase(), "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;
             this.clip.loop();
        }

        public void stopPlay()
    {
       clip.stop();
       this.shouldStop = true;
    }

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


   }

 //Stop the applet
   public void stop()
     {
        player.stopPlay();
     }




    //Destroy the applet
    public void destroy()
    {
       clip.stop();
    }
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
InteractiveMindCommented:
Hm.. Perhaps it's because it's multi-threaded.. only one of the threads is being stopped.
Try this for the stop() method instead:


     public void stop()
     {
        player.stop();
     }

0
 
InteractiveMindCommented:
and also for the destroy method, just to be safe:

    public void destroy()
    {
       player.stop();
    }
0
 
pigmentartsAuthor Commented:
i get an error because the .stop isnt defined i think


 
class ClipPlayer implements Runnable {
        private AudioClip clip;
        private boolean shouldStop;
        private boolean stop;   <----------  I tried to take some initiative adding this line.

My initiative failed.  Where is this declared??
0
 
InteractiveMindCommented:
Right... you don't need to declare anything with your ClipPlayer, in order to use the stop() method, because it implements Runnable (and same applies when you extend the Thread class), the stop() method stops the thread from running..
Try this, to hopefully prevent any errors:

     public void stop()
     {
        if ( player != null )
           player.stop();
     }

     public void destroy()
     {
        if ( player != null )
           player.stop();
     }

0
 
pigmentartsAuthor Commented:
Still get the same errors i'm afraid.

-------------------------------------------------------------
About the thread:: you said ----> (and same applies when you extend the Thread class)

I can see where thread has been used

 new Thread(player).start();

but i dont quite understand how it affects the class ClipPlayer.....it must be something to do with the .start(); method??
0
 
InteractiveMindCommented:
Okay, very strange.. :o\

Perhaps this is a better way to do what you want here...


import java.awt.*;
import java.applet.AudioClip;

public class Looper extends javax.swing.JApplet implements Runnable
{
     AudioClip bgSound;
     Thread runner;
     
     public void init()
     {
          bgSound = newAudioClip(new java.net.URL(getCodeBase(), "1000Hz.au"));
     }
     
     public void start()
     {
          if ( runner == null )
          {
               runner = new Thread( this );
               runner.start();
          }
     }
     
     public void stop()
     {
          if ( runner != null )
          {
               if ( bgSound != null )
                    bgSound.stop();
          runner = null;
     }
     
     public void run()
     {
          if ( bgSound != null )
               bgSound.loop();
     }
     
     public void paint( Graphics screen )
     {
          Graphics2D screen2D = (Graphics2D)screen;
          screen2D.drawString( "Playing sounds . . .", 10, 10 );
     }
}


Just an idea.. let me know if it works :)
0
 
pigmentartsAuthor Commented:
Not sure why but its says illegal start  for

 public void run()#

the last two brakets also come up as errors
0
 
InteractiveMindCommented:
Oh, sorry.. change the stop() method, so it's this instead:

     public void stop()
     {
          if ( runner != null )
          {
               if ( bgSound != null )
                    bgSound.stop();
               runner = null;
          }
     }

That should solve it
0
 
pigmentartsAuthor Commented:
class Looper is public, should be declared error

shouldn't

public class Looper extends javax.swing.JApplet implements Runnable             be
public class PlaySound extends javax.swing.JApplet implements Runnable         the name of my java file?  I tried changing this but got more errors

0
 
pigmentartsAuthor Commented:
ok i changed it to this, no errors the application runs fine, but the browser still won't stop making noise :P

package jas;

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

public class PlaySound extends javax.swing.JApplet implements Runnable
{
     AudioClip bgSound;
     Thread runner;

     public void init()
     {
        try {
            bgSound = newAudioClip(new java.net.URL(getCodeBase(), "1000Hz.au"));
        } catch (MalformedURLException ex) {
        }
     }

     public void start()
     {
          if ( runner == null )
          {
               runner = new Thread( this );
               runner.start();
          }
     }

     public void stop()
     {
          if ( runner != null )
          {
               if ( bgSound != null )
                    bgSound.stop();
               runner = null;
          }
     }


     public void run()
     {
          if ( bgSound != null )
               bgSound.loop();
     }

     public void paint( Graphics screen )
     {
          Graphics2D screen2D = (Graphics2D)screen;
          screen2D.drawString( "Playing sounds . . .", 10, 10 );
     }
}
0
 
InteractiveMindCommented:
Yeah, it needs to be the same name as your file.. what other errors are you getting?
0
 
InteractiveMindCommented:
> but the browser still won't stop making noise :P

!??!?!?!?!?!?!?!  lol

Hmm...
0
 
pigmentartsAuthor Commented:
What about using design mode to place a button in the browser that stops it
0
 
InteractiveMindCommented:
I'll be back shortly...
0
 
pigmentartsAuthor Commented:
ok so

thanks for telling me :)
0
 
InteractiveMindCommented:
Right, I'm back.
Let me just review it all, and give something a go....
0
 
pigmentartsAuthor Commented:
ok, thx Interactive
0
 
InteractiveMindCommented:
This is really, very strange... I'm running this on my localmachine:


import java.awt.*;
import java.applet.AudioClip;

public class PlaySound extends javax.swing.JApplet implements Runnable
{
     AudioClip bgSound;
     Thread runner;

     public void init()
     {
          bgSound = getAudioClip(getCodeBase(), "aggr.wav");
     }
      
     public void start()
     {
          if ( runner == null )
          {
               runner = new Thread( this );
               runner.start();
          }
     }

     public void stop()
     {
          if ( runner != null )
          {
               if ( bgSound != null )
                    bgSound.stop();
               runner = null;
          }
     }

     public void run()
     {
          if ( bgSound != null )
               bgSound.loop();
     }

     public void paint( Graphics screen )
     {
          Graphics2D screen2D = (Graphics2D)screen;
          screen2D.drawString( "Playing sounds . . .", 10, 10 );
     }
}


And it plays fine; it also stops whenever I go to another page, or exit the browser..

This is very very odd.  :o\
0
 
CEHJCommented:
This works fine for me:


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

public class PlaySound extends Applet implements ActionListener {
      private AudioClip clip;
      private Button bnStart, bnStop;


      // 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(getCodeBase(),
                              "1000Hz.au"));
                  bnStart = new Button("Start");
                  bnStop = new Button("Stop");
                  bnStart.addActionListener(this);
                  bnStop.addActionListener(this);
                  add(bnStart);
                  add(bnStop);
            } catch (Exception e) {
                  e.printStackTrace();
            }
      }
      public void actionPerformed(ActionEvent e) {
            if (e.getSource() == bnStart) {
                  clip.loop();
            }
            else {
                  clip.stop();
            }
      }

}
0
 
InteractiveMindCommented:
There you go, pigmentarts  --  someone who actually knows what they're talking about  :-)
0
 
pigmentartsAuthor Commented:
haha ok i'll try this  :)
0
 
pigmentartsAuthor Commented:
ah wow, thats great thanks interactive and thankyou CEHJ that works fine
0
 
CEHJCommented:
:-)
0
 
InteractiveMindCommented:
Thank you very much  :-)
0
 
InteractiveMindCommented:
CEHJ, while you're here, I was very much hoping that you could perhaps contribute to a thread of mine:
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21392004.html

I'm eager for some professional input... If you could just spair a few minutes, that would be great. Thanks.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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