Solved

Best way to repeat sounds in quick succession?

Posted on 2004-10-11
13
216 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:agrees
Comment Utility
> 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count8 challlenge 13 84
strCopies  challenge 17 73
Unable to start eclipse ? 17 82
get weblogic logged in user in java 2 40
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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

743 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

17 Experts available now in Live!

Get 1:1 Help Now