actionscript-3flex3

Loading sound filein as3


I need to load a sound file on my project but it must be loaded and called only once at the start of executing the code.One way to do this is using CSS but CSSis consuming high bit rate so how can i do this?


Solution

  • I think you must read the AS3 doc: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/Sound.html

    As you can see you just have to create a new Sound object:

    var mySound:Sound = new Sound( new URLRequest('http://url.to.your.sound') );
    

    or if you want to wait for the load complete:

    // create sound object
    var mySound:Sound = new Sound();
    // listen for the end of the loading 
    mySound.addEventListener( Event.COMPLETE, _onSoundLoaded );
    // maybe you want to track the progress of the loading
    mySound.addEventListener( Event.COMPLETE, _onSoundLoading );
    // start loading
    mySound.load( new URLRequest('http://url.to.your.sound') );
    
    // the loading is complete
    function _onSoundLoaded(e:Event):void
    {
        // play the sound
        mySound.play();
    }
    
    // the loading progress
    function _onSoundLoading(e:Event):void
    {
        // trace the progression
        trace( mySound.bytesTotal / mySound.bytesLoaded );
    }
    

    Hope this helps.