Looping sounds in ActionScript 3
by iampeterbanjo on March 1, 2009
ActionScript 3 has done quite a few things differently to ActionScript 2. I was doing a lot of programming in C# before I got a chance to play around with it and it looked quite familiar – maybe the whole Object Oriented Programming things is catching on.
Anyway – tried to loop a sound clip in Flash and it was a lot more painful than I expected so I made a note in my work diary that looked like this -
25 February 2009
To loop a sound clip in Flash using ActionScript 3 refer to –
http://doogog.com/music-looping-in-as3.htmlThe code I developed also has a method to stop the music using a button is -
1: //Stop at this frame2: stop();3:4: //Create a new sound object5: var music:Sound = new Sound()6:7: //Set location of sound file – if it is external – using a URLRequest object8: var req:URLRequest = new URLRequest("audio/bongos.mp3");9:10: //Create a new SoundChannel object11: var channel:SoundChannel;12:13: //load music file – via url request object – into channel object14: music.load(req);15: playMusic();16:17: //Boolean checks if the sound is playing18: var musicIsOn:Boolean = true;19:20: //An event listener for a button that stops the music21: btnMusic.addEventListener(MouseEvent.CLICK, stopMusic);22:23: function stopMusic(evt:Event):void {24: if(musicIsOn)25: {26: trace("stop");27: channel.stop();28: musicIsOn = false;29: } else {30: playMusic();31: musicIsOn = true;32: }33: }34:35: function playMusic():void{36: trace("play");37:38: //play music through the SoundChannel39: channel = music.play();40:41: //add an event listener that reacts when the sound being played42: //is completed and repeats the sound43: channel.addEventListener(Event.SOUND_COMPLETE, repeat);44: }45:46: function repeat(evt:Event):void {47: if(channel != null)48: {49:50: //without removing the event listener the sound clip will play only twice51: //I am not sure why but it feels like the event listener gets disposed of52: //once used by default.53: channel.removeEventListener(Event.SOUND_COMPLETE, repeat);54: playMusic();55: }56: }
I hope you found that useful.
9 comments
Thanks, man!
It was really useful for me.
Only there is a little, for half of second interruption between the loops…
by Nio on July 22, 2009 at 01:58. #
To repeat the sound over and over forever:
addEventListener(Event.SOUND_COMPLETE, repeat, false, 0, true);
function repeat(e:Event):void {
sc = music.play(0, int.MAX_VALUE);
}
In some case ( to avoid “Cannot access a property or method of a null object reference”) you need to add this code inside of if:
if (channel) {
//channel.addEventListener….etc.
}
Have a nice day,
by Everin on September 6, 2009 at 14:29. #
Thank you so much for this tutorial. Excellent.
by Jan Clewett on September 13, 2009 at 21:51. #
ur explaintion is just too easy to understand, really help a lot in my project, keep it up ….thanks
by vincent on September 24, 2009 at 01:38. #
thanks for the feedback.
by iampeterbanjo on September 25, 2009 at 12:00. #
wow this is really work.. thaks alo…
thanks
by callmewim on October 17, 2009 at 08:14. #
You’re welcome.
by iampeterbanjo on October 17, 2009 at 10:26. #
thank you so much, you helped me out a bunch with this!
by redag on January 8, 2010 at 17:35. #
really helpful one , also thanks to Everin.
by vinod on June 3, 2010 at 02:20. #