Solved

Best way to repeat sounds in quick succession?

Posted on 2004-10-11
13
218 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 3 50
topping1 challenge 7 73
looking for unix program to search some keywords in side EAR WAR and JAR files 10 80
eclipse compiler vs Installed JREs option 3 76
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now