I would like to ask you how to use the java.applet.AudioClip. I have tried to use it, but I could not figure out how to use it. Thanks for your help.
Asked
Active
Viewed 5,245 times
1 Answers
0
Here is how I implement the AudioClip class:
AudioClip clip = Applet.newAudioClip(url);
Where url is the URL object that points to my sound file. You can get the URL object multiple ways, but I use this (because it always works for me):
URL url = getClass().getClassLoader().getResource("sound1.wav");
And then to play the sound, call the clip's play method on a new Thread:
new Thread() {
public void run() {
clip.play();
}
}.start();
Here's an example of a Sound class you could use to play your audio clips:
public class Sound {
public static final Sound[] sounds = {
new Sound("sound1.wav"),
new Sound("sound2.wav")
};
private AudioClip clip;
private Sound(String name) {
clip = Applet.newAudioClip(getClass().getClassLoader().getResource(name));
}
public void play() {
new Thread() {
public void run() {
clip.play();
}
}.start();
}
}
Just define all of your sounds in the static Sound array, and call those when you need to use the sound:
//anywhere outside of the Sound class
//(will play "sound1.wav" in this example)
Sound.sounds[0].play();
Hope this helped you!

mnickels
- 1