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.

Related Posts

ActionScript 3 Logo

Moving From AS2 to AS3: A Handy List of Resources

Welcome to ActionScript 3.0

ActionScript 1:1 – Getting Intimate With ActionScript 3.0

SWF Framerate Optimization

Smart SWF Optimization with Custom Framerates

ActionScript 3 Migration Cookbook

ActionScript 3 Migration Cookbook

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

Unsigned

By WooThemes

Unsigned is a hugely powerful, cleverly developed, yet super easy theme to use for musicians and bands looking to showcase their music, tours, photos, videos and band members all from within WordPress. It even allows you to sell your albums and merchandise thanks to the WooCommerce integration. It truly is a massive theme that will cover your every need.

Theme Categories: Multimedia, Responsive

Latest Tweets