Solved

Best way to repeat sounds in quick succession?

Posted on 2004-10-11
13
223 Views
Last Modified: 2010-03-31
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
Comment
Question by:agrees
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 

Author Comment

by:agrees
ID: 12282211
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
 
LVL 8

Accepted Solution

by:
pjcrooks2000 earned 125 total points
ID: 12282667
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
 

Author Comment

by:agrees
ID: 12287601
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
Independent Software Vendors: 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!

 

Author Comment

by:agrees
ID: 12287641
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
 
LVL 8

Expert Comment

by:pjcrooks2000
ID: 12287985
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
 
LVL 8

Expert Comment

by:pjcrooks2000
ID: 12288002
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
 

Author Comment

by:agrees
ID: 12288221
> 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
 
LVL 8

Expert Comment

by:pjcrooks2000
ID: 12288343
Ok sure no problems it's not always evident want your trying to do sat here!

Best of luck with it :)
0
 

Author Comment

by:agrees
ID: 12288586
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
 

Author Comment

by:agrees
ID: 12288641
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
 
LVL 8

Expert Comment

by:pjcrooks2000
ID: 12291053
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
 

Author Comment

by:agrees
ID: 12297514
> 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
 
LVL 8

Expert Comment

by:pjcrooks2000
ID: 12297741
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

688 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