Question

I'm currently working on a project in flashdevelop and I want to include some music. I'm building a game, so multiple files are not an option. Currently all resources are embedded and a simple preloader loads everything (like the flashdevelop default preloader). I don't want to load the music at the beginning, I'd rather like to stream it when required.

Is it possible to stream embedded sounds? If not, is it possible to embed these files inside the .swf file and load them later on?

Thanks in advance!

Was it helpful?

Solution

You can do two things. One is to start loading the sounds after the initial loading finishes and save them in a Dictionary maybe. Second is to export a RSL (Runtime Shared Library) from Flash which is a SWF file which you can then load and have access to all the classes defined there.

In the first approach you basically load every sound like this and save them to dictionary:

import flash.media.Sound;
import flash.events.Event;
import flash.net.URLRequest;
import flash.utils.Dictionary;

var mSounds:Dictionary = new Dictionary();

function loadSound(url:String, soundName:String)
{
    var sound:Sound = new Sound();
    sound.addEventListener(Event.COMPLETE, onSoundLoadComplete);
    sound.load(new URLRequest(url));

    function onSoundLoadComplete(e:Event):void
    {
        sound.removeEventListener(Event.COMPLETE, onSoundLoadComplete);
        trace(soundName,"Sound Loaded");

        mSounds[soundName] = sound; // save it to dictionary

        // then you can load it from dictionary 
        // using the name you assigned
        if(mSounds["crystalised"])
            (mSounds["crystalised"] as Sound).play();
    }
}


loadSound("C:\\Users\\Gio\\Desktop\\Crystalised.mp3", "crystalised");

In the second approach you have to do more steps, but you load it once. I'll list the steps here:

  1. Make a new Flash Document (FLA)
  2. Import all the sounds you need to the library
  3. In the properties menu of each sound select the Actionscript tab and tick the Export for Runtime Sharing checkbox and fill in the name for output SWF Export for Runtime Sharing
  4. After you publish this FLA you can load it in your application or game and use it like this:

    import flash.display.Loader;
    import flash.system.LoaderContext;
    import flash.system.ApplicationDomain;
    import flash.system.SecurityDomain;
    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.media.Sound;
    import flash.utils.getDefinitionByName;
    
    function loadRSL(url:String):void
    {
        var loader:Loader = new Loader();
        var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
    
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onRSLLoadComplete);
        loader.load(new URLRequest(url), context);
    
        function onRSLLoadComplete(e:Event):void
        {
            loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onRSLLoadComplete);
            trace("RSL Loaded");
    
            // creating a new instance of the sound which is defined in RSL
            var soundClass:Class = getDefinitionByName("Crystalised") as Class;
            var sound:Sound = (new soundClass() as Sound);
            sound.play();
        }
    }
    
    loadRSL("SoundLibrary.swf");
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top