The web site for Flash ActionScript 3.0 game developers

 
         
   

Ask Gary: How to Play Multiple Sounds

Hi Gary,
Thanks for the podcasts. I've just watched your video podcast on using sound in AS3 and it was very good, but I would like to be able to play more than one sound at a time. I've been able to do this in previous versions of ActionScript, but I can't get it to work in AS3. The new classes for producing sounds has changed quite a bit and I find it a little confusing.
Thank you for your time,
Chris Schnuck

Thanks for the question Chris. In the past, playing multiple sounds at the same time has been tough in Flash. Each movie clip, including the root level, could only handle one at a time. But with AS3, this doesn't seem to be an issue.

Using similar code from the Podcast, I was able to have a loop playing and also have sounds playing from a button at the same time. I just basically created two different sound objects to do it. The music loop uses one called myLoopSound and the button sounds uses one called myExternalSound.

myButton.addEventListener(MouseEvent.CLICK, playSound);

var myExternalSound:Sound = new Sound();
myExternalSound.load(new URLRequest("SoundExample.mp3"));

var myLoopSound:Sound = new Sound();
myLoopSound.load(new URLRequest("MusicLoop.mp3"));
myLoopSound.play();

function playSound(event:Event) {
myExternalSound.play();
}

You can download the example movie here.



Copyright Gary Rosenzweig