External Sound

Loading External Sounds Using AS3

Tags: , ,

Sound has become a key addition to many Flash projects: offline and online. In many cases, integrating sound can actually amplify the final results of a project. However, in many other cases sound should not even be a topic for discussion.

Loading external sound into your Flash projects using AS2 is a fairly easy task.  Using AS3 to load external sounds is just as simple.  I am going to quickly go over how to load an external sound file using AS3 in comparison to AS2 syntax.

Loading external sound using AS2

//Create a new sound object
var song:Sound=new Sound();
//Load the external sound file
song.loadSound("song.mp3",true);
//Once loaded play the sopund file
song.onSoundComplete=function() {
	song.start();
}

Loading external sound using AS3

//Create an instance of the Sound class
var soundClip:Sound=new Sound();
//Create a new SoundChannel Object
var sndChannel:SoundChannel=new SoundChannel();
//Load sound using URLRequest
soundClip.load(new URLRequest("song.mp3"));
//Create an event listener that wll update once sound has finished loading
soundClip.addEventListener(Event.COMPLETE,onComplete,false,0,true);
function onComplete(evt:Event):void {
	//Play loaded sound
	sndChannel=soundClip.play();
}

What is the difference?

Event model consistency

Loading external sound and playing it using AS3 is quite similar to that of AS2 syntax. The key difference between the two would have to the new event model integration that AS3 brings to the table.

Introducing Sound Channel

One other difference is the declaration and use of a Sound Channel. The SoundChannel class is used to create a separate channel for each new sound played. By placing each sound in its own channel, you can work with multiple sounds but control each sound separately.

Conclusion

Overall, the changes between AS2 to AS3 in loading external sound are not far off from each other. However, AS3 does introduce a boat load of new features and classes that allow for creative uses of sound in your Flash Projects. Some of the other new features include: visualizing sound data, reading ID3 Metadata, and transformation of the sound files.

Download Source

Subscribe to the RSS Feed to stay updated on future tutorials on using sound in AS3.

I hope this helps in your migration over into  AS3.  If there is something in particular you want to add or see with the Moving From AS2 to AS3 series, feel free to contact me via the contact page.

9 Comments

  1. Arunraj

    03.13.2009

    Reply

    nice and simple one…….thank you

  2. Paolo

    03.19.2009

    Reply

    If I wanted more than one sound to play/load, do I just copy the code for each new sound?

  3. Bill K

    08.07.2009

    Reply

    Does the sound stream or would you need a preloader?

    • Angel

      08.09.2009

      Reply

      Yeah, you would definitely need a preloader! Implementing one would not be difficult at all.

    • Angel Romero

      10.03.2010

      Reply

      Yep, preloader needed!

  4. Maxwell K

    10.23.2009

    Reply

    Nice and Simple!

  5. CHELVN

    11.18.2009

    Reply

    thx, so simple :x

  6. Danie

    04.30.2011

    Reply

    You have no idea how long ive been looking for this…….God bless you. Seriously.

Leave a Reply









Latest Theme From WooThemes

Capital

By WooThemes

Capital is a WooCommerce theme which has been designed specifically to sell digital products. There are options to strip functionality right back which makes this the perfect theme for boutique software agencies. Capital has also been fully optimised for use on hand-held, touch screen devices.

Theme Categories: Responsive, WooCommerce

Latest Tweets