Author: David Ford
Plays background sounds for web page in random order at random intervals. Perfect for setting the mood of a page.
Features
1. Random delay factor and order add to the effect by simulating real life background noise.
2. Can be customized with any number of sounds.
3. Each sound is given a priority as to how often it plays and a delay factor to determine the relative length of silence follow the sound.
4. Playback can be paused by clicking on the applet. BackSound displays either a image or the background color may be specified.
BackSound.java
// BackSound by David Ford (c) 1999
//
// BackSound plays sounds from playlist in random order with a random interval
// between sounds. A priority is given for each sound to determine it's freqency
// A delay is given for each sound to determine the relative length of pause after
// the sound plays. Soundn will play aproximately priorityn time out of (total
// total priority). Delay after playing soundn will be 1*delayn to 20*deyaln seconds.
// BackSound will display either a blank background of color or the image given.
// WIDTH & HEIGHTH should match the size of image if it is used. If image is used
// then color will have no effect. Play stops when the mouse is clicked on the Applet
// and resumes when the mouse is clicked again.
//
// <APPLET CODE = "BackSound.class" WIDTH=100 HEIGHT=100>
// <!--WIDTH & HEIGHT must match width & height of image if used-->
// <PARAM NAME="numsounds" VALUE="4">
// <PARAM NAME="sound1" VALUE="cat.au">
// <PARAM NAME="delay1" VALUE="3">
// <PARAM NAME="priority1" VALUE="3">
// <PARAM NAME="sound2" VALUE="dog-1.au">
// <PARAM NAME="delay2" VALUE="2">
// <PARAM NAME="priority2" VALUE="2">
// <PARAM NAME="sound3" VALUE="Gun-1.au">
// <PARAM NAME="delay3" VALUE="1">
// <PARAM NAME="priority3" VALUE="1">
// <PARAM NAME="color" VALUE="black"> // not used if image is given
// <PARAM NAME="image" VALUE="Back.gif">
//
// This Applet is my own original work. BackSound is free for distribution to
// non-commercial sites. If you would like to use this applet on anyother site then
// all I ask is that you e-mail me at Shade2020@aol.com and let me know that my work
// is appriciated. If you would like to use this applet on a commercial site then
// e-mail me and request a liscence to do so. ~ David
import java.applet.*;
import java.lang.Thread.*;
import java.util.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
public class BackSound extends Applet implements Runnable {
int NumSounds; //Number of sound clips
AudioClip Sounds[]; //Array of sounds
long Delay[]; //Delay time after each sound
int Priority[]; //Priority of each sound
int TotalPriority; //Total of all Priorties
Image BGImage; //background image
Color BGColor; //background color
Thread Go; //The thread to play sounds
boolean Running = false;//Is the thread running?
boolean Active = true; //Has the Applet been destroyed?
//About information.
public String getAppletInfo() {
return
"BackSound - a background sound effect player.\n"
+"Plays random sound from list in random order at random intervals.\n"
+"Click cause sound playback to toggle.\n"
+"version 2.0.0\n"
+"(c) 1999 - David Ford\n\n"
+"numsound - Number of sounds in list\n"
+"soundn - .au file n\n"
+"delayn - pause delayn to 20 x delayn seconds after soundn\n"
+"priorityn - play soundn this number of times from total of priorities\n"
+"image - graphics file to display\n"
+"color - background color if image not defined\n"
+"Color may be given as rrr.ggg.bbb, rrrgggbbbb or by name";
}
String pinfo[][] = {
{"numsounds","integer","number of sounds in list"},
{"sound1","URL","1st sound file"},
{"sound2","URL","2nd sound file"},
{"...","...","repeat soundn for each sound file 1 through numsounds"},
{"priority1","integer","play sound1 priority1 in total priorities times"},
{"priority2","integer","play sound2 priority2 in total priorities times"},
{"...","integer","repeat priorityn for each sound file 1 through numsounds"},
{"delay1","long","pause delay1 to 20x delay1 seconds before playing next sound"},
{"delay2","long","pause delay2 to 20x delay1 seconds before playing next sound"},
{"...","long","repeat delayn for each sound file 1 through numsounds"},
{"image","URL","image to display"},
{"color","string","rrr.ggg.bbb where rrr = red value, ggg = green & bbb = blue"},
{"color","integer","rrrgggbbb color value"},
{"color","string","color name"}
};
public String[][] getParameterInfo() {
return pinfo;
}
public void init () {
int PriorityIn = 0;
//read in the background image or color
if (this.getParameter("image") != null) {
BGImage =getImage(this.getCodeBase(), this.getParameter("image"));
}
else {
int red;
int blue;
int green;
String ColorIn;
StringTokenizer st;
ColorIn = this.getParameter("color");
st = new StringTokenizer(ColorIn,".");
//Color is not in form rrr.ggg.bbb
if (st.countTokens() < 3) {
//Color might be given in form rrrgggbbb
try {BGColor = new Color(Integer.parseInt(ColorIn));}
//Color might be given by name
catch (NumberFormatException nfebgc) {
BGColor = Color.getColor(ColorIn);
setBackground(BGColor);
}
}
//color is in form rrr.ggg.bbb
//if any of these values are bad then default to 0
else {
try {red = Integer.parseInt(st.nextToken());}
catch (NumberFormatException nfer) {red = 0;}
try {green = Integer.parseInt(st.nextToken());}
catch (NumberFormatException nfeg) {green = 0;}
try {blue = Integer.parseInt(st.nextToken());}
catch (NumberFormatException nfeb) {blue = 0;}
BGColor = new Color(red, blue, green);
setBackground(BGColor);
}
}
//read the number of sounds and create the sound data arrays
NumSounds = Integer.parseInt(this.getParameter("numsounds"));
if (NumSounds < 1) {
showStatus ("Invalid number of sounds");
NumSounds = 1;
}
Sounds = new AudioClip[NumSounds];
Delay = new long[NumSounds];
Priority = new int[NumSounds];
//get the sound data and load the sounds
for (int i = 0; i < NumSounds; i++) {
//Get the .au file, skip if bad
try {Sounds[i] = getAudioClip(this.getDocumentBase(), this.getParameter("sound"+String.valueOf(i+1)));}
catch (NegativeArraySizeException nases) {
Delay[i] = 0;
Priority[i] = 1;
continue;
}
//Get the delay factor default to 1 if bad
try {Delay[i] = Long.parseLong(this.getParameter("delay"+String.valueOf(i+1)));}
catch (NumberFormatException nfed) {
}
if (Delay[i] == 0) Delay[i] = 1;
//Get the priority factor default to 1 if bad
try {PriorityIn = Integer.parseInt(this.getParameter("priority"+String.valueOf(i+1)));}
catch (NumberFormatException nfep) {
}
if (PriorityIn == 0) PriorityIn = 1;
//Increment total Priority and set priority of this
//sound to the new total
TotalPriority += PriorityIn;
Priority[i] = TotalPriority;
}
//Listen for click and mouse enter and begin
this.addMouseListener(new Listener());
Running = true;
}
//make sure the image or background is shown & start
public void start() {
repaint();
Running = true;
if (Go == null) {
Go = new Thread(this);
Go.start();
}
// else {
// notify();
// }
}
// kill the PlayThread
public void stop() {
Running = false;
}
// kill the PlayThread free the BGImage
public void destroy() {
Active = false;
Running = false;
Go = null;
if (BGImage != null) {
BGImage = null;
}
}
// Play sounds in random order at random intervals
public void run() {
int SoundNumber; //Sounds[] index
int PriorityValue; //Random Priority
long DelayTime; //Delay[] index
while (Active) {
//Generate a random priority
PriorityValue = (int) (java.lang.Math.random() * TotalPriority);
if (PriorityValue == 0) PriorityValue = 1; //shouldn't happen
//find 1st sound of equal or higher priority
for (SoundNumber = 0; SoundNumber < NumSounds; SoundNumber++) {
if (Priority[SoundNumber] >= PriorityValue) {
//Set delay for this sound random 1 to 10 seconds
//mutiplied by sounds delay factor
DelayTime = (long) (java.lang.Math.random() * 10);
if (DelayTime == 0) DelayTime = 1;
DelayTime *= 1000;
DelayTime *= Delay[SoundNumber];
if (DelayTime == 0) DelayTime = 1000;
//play it if it is valid, if not do nothing this pass
try {Sounds[SoundNumber].play();}
catch (NullPointerException npes) {
continue;
}
//pause after playing the sound
try {Thread.sleep(DelayTime);}
catch (InterruptedException ie) {}
break;
}
}
//Check to see if the applet has been paused
// synchronized (this) {
// if (!Running) {
// try { wait(); }
// catch (InterruptedException ie) {}
// }
// }
while(!Running){
showStatus("Paused: Click to resume");
}
}
}
// display background color or image
public void paint(Graphics g) {
if (BGImage != null) {
g.drawImage(this.BGImage,0,0,this);
}
else {
this.setBackground(this.BGColor);
}
}
class Listener extends MouseAdapter {
//toggle pause if Mouse pressed
public void mouseClicked(MouseEvent e) {
if (Running) {
//Pause if playing
showStatus("Pausing: Click to Resume");
Running = false;
}
else {
//play if paused
showStatus("Resuming: Click to Pause");
start();
}
}
//Display messege to indicate current state
//and mouse click action.
public void mouseEntered(MouseEvent e) {
if (Running) {
showStatus("Running: Click to Pause");
}
else {
showStatus("Paused: Click to Resume");
}
}
}
}
Download source from author's site
Download source code
Posted On: 30-Jun-1999