Forum Moderators: open
I wanted to build a metronome in flash for my website. I realized that it would be pretty easy with the setInterval() function.
I finished coding the project and then i tested it out.
I find that it's inconsistent, very slight, but it definantly is. Sometimes it pauses for like a 10th of a second. For a metronome that is suppose to help people stay consistent, this is unacceptable.
Is there something else in flash that i can use to play a short sound over and over again in specific consistent intervals, and is adjustable?
Or is there someway i can optimize the setInterval code to make it more consistent?
Here is the code if you want to see it:
stop();
stopAllsounds();
// load sounds
snare = new Sound();
snare.attachSound("snare");
tick = new Sound();
tick.attachSound("tick");
kick = new Sound();
kick.attachSound("kick");
// default sound
var strSnd:String = "tick";
theSound = new Sound();
theSound.attachSound(strSnd.toString());
// Default BPM
var numBPM:Number = 88;
var numTime:Number = 1000*(60/numBPM);
var nPlaySoundInterval:Number;
// PLAY BUTTON
btnPlay.onRelease = function():Void {
stopAllSounds();
clearInterval(nPlaySoundInterval);
nPlaySoundInterval = setInterval(playSound, numTime);
};
// STOP BUTTON
btnStop.onPress = function():Void {
stopAllSounds();
clearInterval(nPlaySoundInterval);
};
// INTERVAL FUNCTION
function playSound():Void {
theSound.start(0,1);
}
// Beat Per Minute Adjuster OBJECT LISTENER
form = new Object();
form.change = function(eventObj){
numBPM = eventObj.target.value;
var numTime:Number = 1000*(60/(numBPM));
trace(numTime);
clearInterval(nPlaySoundInterval);
nPlaySoundInterval = setInterval(playSound, numTime);
}
bpm.addEventListener("change", form);
// SOUND TYPE OBJECT LISTENER
form2 = new Object();
form2.click = function(eventObj2){
strSnd = eventObj2.target.selectedData;
theSound.attachSound(strSnd.toString());
clearInterval(nPlaySoundInterval);
nPlaySoundInterval = setInterval(playSound, numTime);
}
radioGroup.addEventListener("click", form2);
Thanks