Solved

Best way to repeat sounds in quick succession?

Posted on 2004-10-11
13
222 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

738 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