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

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

Best way to repeat sounds in quick succession?

Hi all,

I would like my application to make a "click" noise when the user moves the mouse, so that if he moves it quickly it goes "c-c-c-c-c-c-click".

With my current code it just goes "click" every so often, but moving the mouse quickly seems to make it click less.  Can anyone improve this?:

private static AudioClip CLICK = Applet.newAudioClip(new URL(baseURL + "TYPEKEY.wav"));

public void mouseMoved(MouseEvent event) {
  if(isMovedFarEnough(event)) {
    CLICK.play();
  }
}
0
agrees
Asked:
agrees
  • 7
  • 6
1 Solution
 
agreesAuthor Commented:
p.s. I've spent ages experimenting with different combinations of invokelater and Timers so please don't just link me to the java tutorial ;)
0
 
pjcrooks2000Commented:
How about running this inside a Thread?  That way you willhave the event dispatch thread running at the same time as your new Thread!

0
 
agreesAuthor Commented:
ok, well I've done that (the code is below) and it seems to work fairly well.  But is creating a new thread for every click not a really resource heavy way of doing things?  Is there not a better way?

public void mouseMoved(MouseEvent event) {
  if(isMovedFarEnough(event)) {
    SoundThread t = new SoundThread();
    t.start();
  }
}


  public class SoundThread extends Thread {
    public void run() {
      Sounds.playClick();
    }
  }

public abstract class Sounds {
  public static void playClick() {
    try {
      AudioClip newClip = Applet.newAudioClip(new URL(baseURL + "TYPEKEY.wav"));
      newClip.play();
    }
    catch (MalformedURLException ex) {
    }
  }
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
agreesAuthor Commented:
Also, is there a way to put the thread creation within a static method in the 'Sounds' utility class?  

I'd prefer to keep all Sounds management incapsulated within the Sounds class, and I'd prefer to keep the sounds class an abstract utility class rather than have to create an instance of it.
0
 
pjcrooks2000Commented:
Should not be any reason why you could not do what your saying above! yes just call the class method from your Panel/frame or whatever your using.

Try using an .au file as I think this will give you better compression rates over a wav file may help with the resources.  You may also want to add another method that gets called when the mouse stops to call newClip.stop();  and you can also call another method newClip.loop(); whilst the mouse is moving in a loop else newClip.stop();

If you see what I mean! You will really want to clip the audio file down as much as you can, most audio files have unessary junk at the beginning and end of them that cause a delay and this will work well with the loop if you can edit it to contant just the sound you want but make it an au file.

Use Cool edit pro thats what I use for editing clips.

Good luck

pjcrooks2000



0
 
pjcrooks2000Commented:
Sorry theres a bit more,

 also the Thread will only be running whilst the mouse is moving, looping instead of creating a new instance of the thread on each movement detection.  One continuos thread for looping the clip and killl the thread even when the mouse stops.

Hope this helps!
0
 
agreesAuthor Commented:
> Should not be any reason why you could not do what your saying above! yes just call the class method from your Panel/frame or whatever your using.
But playClick() is static so I can't create an instance of 'SoundThread' within it.

> also the Thread will only be running whilst the mouse is moving, looping instead of creating a new instance of the thread on each movement

The problem is the click actually means something:  It means the mouse has crossed a line on the Canvas.  So if the mouse moves slowly I want the clicks to be slow, and visa versa - so a loop won't give the effect I'm trying to achieve.

Thanks for the tips on the sound files - I'll try to look for some .au files!
0
 
pjcrooks2000Commented:
Ok sure no problems it's not always evident want your trying to do sat here!

Best of luck with it :)
0
 
agreesAuthor Commented:
I'm happy with this appart from one question:

Is there a way to create a new thread from within a static method?  I really would just like to be able to call playClick() and have that method

  public class SoundThread extends Thread {
   
public void run() {
      Sounds.playClick();
    }
  }

public abstract class Sounds {
  public static void playClick() {
    SoundThread t = new SoundThread(new URL(baseURL + "TYPEKEY.wav"));
    t.start();
    try {
      AudioClip newClip = Applet.newAudioClip();
      newClip.play();
    }
    catch (MalformedURLException ex) {
    }
  }
0
 
agreesAuthor Commented:
Woops ignore that above - it posted before I'd finished!  Here's what I meant to say:

I'm happy with this appart from one question:

Is there a way to create a new thread from within a static method?  I really would just like to be able to call playClick() and have that method deal with creating the thread, like this (which won't compile because it won't let me create an instance within a static method):


public abstract class Sounds {
  public static void playClick() {
    SoundThread t = new SoundThread(new URL(baseURL + "TYPEKEY.wav"));  // Won't compile!!
    t.start();
  }

public class SoundThread extends Thread {
  URL clipURL;
  public SoundsThread(URL clipURL) {
    this.clipURL = clipURL;
  }
  public void run() {
    try {
      AudioClip newClip = Applet.newAudioClip(clipURL);
      newClip.play();
    }
    catch (MalformedURLException ex) {
    }
    }
  }

0
 
pjcrooks2000Commented:
Have a read of this http://www.eimc.brad.ac.uk/java/tutorial/Project/6/intro.htm

Have a look down at CurrentThread, hope that helps you!

0
 
agreesAuthor Commented:
> Have a look down at CurrentThread, hope that helps you!

Not really because I want to create a new thread, not get the current thread!  Never mind, I'll try to get help for this in a new question here:

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21166556.html

Thanks for your help
0
 
pjcrooks2000Commented:
No problem, thanks for accepting.  sorry I could not spend more time on it, currently working on a project.  All the best!

pjcrooks2000
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now