Hi,
I know how to play a sound effect:
var mySoundReq:URLRequest = new URLRequest(“laser.mp3”);
var mySound:Sound = new Sound();
mySound.load(mySoundReq);
mySound.addEventListener(Event.COMPLETE, playSong);
function playSong(event:Event):void {
mySound.play();
}
The above sound effect play only one time. Actually I wish to play the sound effect for 6 times and at a set play interval (e.g. play the sound effect, wait 2 seconds, play again, wait another 2 seconds…). How can I do that?
I am thinking to use a Timer object. However I have no idea how to link with the playSong function.
Thanks and best regards
Alex
you dont have to have a timer, unless you want the delay, as you stated. use a SoundChannel object to monitor playback, to get the playback completed listener.
var mySoundReq:URLRequest = new URLRequest("next2.mp3");
var mySound:Sound = new Sound();
var myPlayer:SoundChannel = new SoundChannel();
var myTimer:Timer = new Timer(2000); //delay
var myRepeat:Number = 3; //repeat count
var myPlayed:Number = 0;
mySound.load(mySoundReq);
//loading done.
mySound.addEventListener(Event.COMPLETE, startPlay);
//playback done.
myPlayer.addEventListener(Event.SOUND_COMPLETE,resumeSound);
//clockradio timer.
myTimer.addEventListener("timer",playSound);
function startPlay(event:Event):void {
//only need to start the timer.
myTimer.start();
}
function resumeSound(e:Object):void {
//check repeat count and if is all good start the timer again, otherwise leave it.
trace(myPlayed + " of " + myRepeat);
if (myPlayed < myRepeat) {
myTimer.start();
}
}
function playSound(e:Object):void {
//kill the timer so you wont get overlapping
myTimer.stop();
//set the soundchannel object to watch the sound.
myPlayer = mySound.play();
//add the event for playback finished
myPlayer.addEventListener(Event.SOUND_COMPLETE,resumeSound);
//increment the play counter.
myPlayed++;
}
Hi rbnzdave,
Thanks for your help. It works exactly what I need:)
Best regards
Alex