Loading sound into Flash applications

This is a long time since I worked on sound on Flash. Actually the sound API itself is very good but the problem is loading. Yep, except the known issue of Sound class and sound complete event, the API itself is cool. The loading of Sound and initialising the Sound Object is not as straightforward as initialising any Display Object. For loading Display Objects I generally use a baked SWF and then load the SWF into my application container. Cast the SWF into the Display Object Class, which is the document class for the baked SWF and then access the properties and methods of it.(More about this in coming blogs). If we take the same route to load the audios from a baked SWF, it does not work!!  The solution would be either attach each sound to a class and write a blank class to attach to or do not write the class and get the sound object with the utility method “getDefinitionByName()”. Both of these ways are too much of a work for a little output.

While searching for the solution I came across “bulk-loader“. Wow, it handles all kinds of load, for me, it does the trick for the audio files loading.  This library provides events for each asset and dispatches event when all the assets are loaded. Cool stuff and well done “debert “, all the best to him.

While loading the assets, I will talk about sound here, but all the assets are handled the same way.While loading the assets, just add them to the bulkloader instance with an id. On all assets load complete event, move on with your application.Wherever you need to access the loaded sound, just get it with the “id” you specified previously and it will return a Sound Object.

Happy Flashing :)


8 thoughts on “Loading sound into Flash applications

  1. Hey Saumya,

    Another AS 3.0 library I really like is CasaLib (http://casalib.org/). Don’t if you have seen it yet. I feel that this library is one that should be used all the time in AS 3.0 projects, simply because it makes dealing with destroying event handlers and objects very simple. This lib is awesome at making sure you clear out event listeners for objects.

    Plus it has a lot of cool utils built into the lib. Check out the org.casalib.load package in the docs (http://as3.casalib.org/docs/). I don’t know if it is as robust as the bulk loader in this respect, but it might be worth checking out. Either way, CasaLib will make your life easier. :)


  2. Hi Scottae,
    I read about CasaLib and keeping an eye on the same as to delve deep into it as I will get time. Unfortunately it seems I am missing out good stuff keeping this for a future todo task.
    Thank you for mentioning it, I will take a look at it as soon as possible.(oops, still it goes to the future :( .I hope I could read it now!! )

    Happy Flashing :)

  3. Never once has anyone I know ever said that the Sound API in AS3 is “very good”.

    Everyone’s entitled to their opinion, and mine is that you are currying favor with Adobe by telling them that one of the worst APIs ever written is “very good”, because nobody thinks the AS3 Sound API is remotely acceptable except the people who wrote it.

  4. hi Steven Sacks,
    I completely agree with you regarding the sound API. But my point here is the “Bulk Loader”. Instead of starting a debate on Sound API I have tried my best to put forth the loading Mechanism of Bulkloader.

  5. I set up bulk-loader locally and got it to load an swf, but I still can`t get to the sound in my library. I just want to assign the sound in the loaded swf`s library to a sound instance variable. Can anyone help with a line of code or two that will do this?

    btw – I am building an 8 channel mixer where the sound for each channel can be loaded on the fly when a user selects an item in an xml driven list box. As you know, flash is terrible at looping mp3s imported at runtime because they have beginning and ending silence (which I have given up trying to remove with LAME encoder tricks)! Since Flash does an outstanding job of converting .wav files into perfectly looping .mp3 files, I finally decided to wrap all my audio loops in individual .swf files. Any suggestions as far as the soundness of this time consuming method of getting Flash to properly loop a loaded mp3 will be greatly appreciated!

    A book devoted PURELY to working with audio in Flash is long overdue!!!

    1. Hi Matt, I would suggest you to load the sounds upfront before the application begins.Then play it whenever needed.Bulk loader best works (and I think should work that way only), when things are loaded in bulk. Once you have all your audio assets ready, you can play any audio, in whichever channel you want it to.
      Regarding the looping thing,just listen to the “SOUND_COMPLETE” event and restart the audio.
      I just put up the source code of the audio controller and the basic usage example, in google code, hope that helps you in any way.

      1. Saumya,

        Thanks for the very informative reply! I`m fairly new to loading assets into Flash at runtime, so this is really helping me!

        I`ve been thinking about my project and if I load all the sounds in bulk, then memory usage will get quite large and it will defeat the purpose (and flexibility) of trying to load the sounds in as users select them! If bulk loader works best when things are loaded in bulk, then I should probably just go ahead and use the standard AS3 loading method. For loading 1 item at a time, using Bulk Loader isn`t really much different (or less code intensive) than using the standard method anyway! Would you agree?

      2. Hey Matt, I agree that the purpose of loading the needed assets whenever necessary is good.But then imagine a situation where you do not want your users to wait in the middle of an application.There comes the need to load the assets before hand only.
        Now for the audio part, the way Flash behaves with audio files embedded inside SWF’s Library is, one has to put all the audio files in the first SWF or the application container.If we try to load a SWF which contains library assets of audio files with linkage ids associated to them, then too the audios do not come to the main application. This is a very daunting and frustrating experience working with audios in Flash. There come bulk loader to help us. It loads the audios whenever you need and completely removes any work arrounds.
        I agree with you regarding the just one assets load, you are better with native actionscript method.
        Thanks again for your kind words,I am glad that information helped you.
        Happy Flashing

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s