package com.hatherly.babykeyboardfun;
/**
* Factory to generate a sound when requested using a class
* implementing the BabySoundPlayer interface
*/
public class SoundFactory {
private final int noOfClips = Constants.audioFilenames.length;
private BabySoundPlayer[] players = new BabySoundPlayer[noOfClips];
private int playerIndex = 0;
private long lastSoundTime = 0;
/** Creates a new instance of SoundFactory */
public SoundFactory() {
System.out.println("Loading Sounds");
for (int n=noOfClips-1; n>=0; n--) {
players[n] = new SoundPlayer_AppletAudio(n);
}
System.out.println("Sounds Complete");
}
/**
* Factory method - called to make a sound
*/
public void makeSound() {
// Only play a sound if it has been more than X milliseconds since the last one
long currentTime = System.currentTimeMillis();
if ((currentTime - lastSoundTime) > 800) {
players[playerIndex++].makeSound();
if (playerIndex == noOfClips) {
playerIndex=0;
}
lastSoundTime = currentTime;
}
}
}
syntax highlighted by Code2HTML, v. 0.9.1