/*
This is a very simple little "play some random audio
clips" applet. It will randomly select one sound from
a list of audio files given as parameters and play it
at applet initialization. It'll play another, random
sound clip if the button is clicked. Also, so you know
it's doing something, the button label changes from
"Play" to "Loading" when it's getting the audio clip.
I felt the need to write this simply because the only
other applets I could find out there either would only
play ONE clip ONCE, or would play multiple clips with
a little jukebox display and funky lights and (If you
had a 32-bit display) holographic projections of Pamela
Anderson dancing to the music, but none of them would
simply play a randomly selected sound clip at startup,
and then another, randomly selected sound clip when
someone hit the button. If there *is* something else
like that out there, then you should advertise better.
:)
Here's an example of how to use it:
A simple sound player
The applet takes two parameters:
...
...
There must be 'n' Clip parameters, or the thing will probably
not work right. The filenames are relative to the codebase of
the applet. (In other words, put the audio files in the same
directory or a subdirectory of the .class file and only spell
out the relative, not absolute, filename.)
If you have any questions, you can write to me at:
dglidden@illusionary.com
and I might even respond.
This code is Copyright 1997 by Derek Glidden, All Rights Reserved,
any use of this code implies that the user is willing to accept all
consequences, including that their wife will leave them, their dog
will die, their truck will break down and they will become a
third-rate country singer, yadda, yadda, yadda, etc, etc, ad infinitum.
I also apologize for the instructions being
more lines than the actual code.
*/
import java.awt.*;
import java.applet.*;
public class soundPlay extends Applet {
java.awt.Button PlayButton;
java.applet.AudioClip mySoundClip;
java.lang.Integer numClipsParam;
int numClips;
String clipName;
private void soundPlayer() {
if(mySoundClip != null) {
mySoundClip.stop();
}
PlayButton.setLabel("Loading");
int whichClip = (int)Math.floor(Math.random() * numClips) + 1;
clipName = getParameter("Clip" + whichClip);
System.out.println("Playing clip \"" + clipName + "\"");
mySoundClip = getAudioClip(getCodeBase(), clipName);
mySoundClip.play();
PlayButton.setLabel("Play");
}
public void init() {
super.init();
setLayout(null);
addNotify();
resize(50,24);
PlayButton = new java.awt.Button("Play");
PlayButton.reshape(0,0,50,24);
add(PlayButton);
numClipsParam = new java.lang.Integer(getParameter("NumClips"));
numClips = numClipsParam.intValue();
System.out.println("Number of audio clips reported = " + numClips);
this.soundPlayer();
}
void PlayButton_Clicked(Event event) {
this.soundPlayer();
}
public boolean handleEvent(Event event) {
if (event.target == PlayButton && event.id == Event.ACTION_EVENT) {
PlayButton_Clicked(event);
return true;
}
return super.handleEvent(event);
}
public void stop() {
if(mySoundClip != null) {
mySoundClip.stop();
}
super.stop();
}
}