SoundManager

Kind of class:class
Inherits from:none
Classpath:org.asapframework.management.sound.SoundManager
File last modified:Monday, 09 October 2006, 00:41:07
SoundManager provides a single access point to all sounds in a Flash project.
Sounds may be compiled into a SWF movie (internal) or loaded as MP3 files (external).
SoundManager allows access to these sounds from any part in the Flash project.
Usage:
  • import org.asapframework.management.sound.SoundManager;
    
    var theSoundManager:SoundManager = SoundManager.getInstance();
    // played sounds need a MovieClip
    var mc:MovieClip = createEmptyMovieClip("soundmanager", 0);
    
    theSoundManager.addMovie("main", mc);
    
    theSoundManager.addSound("test10", "main");
    theSoundManager.addSound("test11", "main");
    
    theSoundManager.playSound("test10");
    theSoundManager.playSound("test11");
    
    onUnload = function () {
        theSoundManager.removeMovie("main");
    }

Summary


Constructor
Class properties
Instance properties
Class methods
Instance methods
  • addMovie (inName:String, inClip:MovieClip) : Boolean
    • Add a named movieclip to function as container for sounds Make sure the movieclip resides in the same swf as the sounds that will be added to it.
  • removeMovie (inName:String) : Void
    • Remove a previously added movie with specified name.
  • addSound (inLinkageName:String, inMovieName:String) : Boolean
    • Add a sound from the library, associated with the specified movie name
  • addExternalSound (inLinkageName:String, inMovieName:String, inUrl:String, inIsStreaming:Boolean, inListener:Object) : Boolean
    • Add an external sound.
  • removeSound (inName:String) : Void
    • Remove a previously added sound
  • removeSoundsInMovie (inMovieName:String) : Void
    • Remove all sounds in a previously added sound container movie
  • playSound (inName:String, inPlayCount:Number) : Boolean
    • Play a sound with the specified name and play count.
  • stopSound (inName:String) : Boolean
    • Stop the sound with the specified name
  • setSoundVolume (inName:String, inVolume:Number) : Boolean
  • stopAllSounds : Void
    • Stop all sounds.
  • muteAllSounds : Void
    • Set the volume of all sounds to 0.
  • unmuteAllSounds : Void
    • Reset the original volume of all sounds.
  • getSound (inName:String) : SoundClip
    • Get the SoundClip object for more direct manipulation.
  • getSoundIndex (inName:String) : Number
    • Return the index of the SoundClip object with the specified name.
  • getMovie (inName:String) : SoundMovie
    • Return the movie with the specified name.
  • getMovieIndex (inName:String) : Number
    • Return the index of the movie with the specified name
  • createSoundClip (inLinkageName:String, inMovieName:String) : SoundClip
    • Create a new SoundClip object with the specified name in the specified movie
  • toString : String

Constructor

SoundManager

private function SoundManager (
)

Private constructor, use getInstance to refer to singleton instance.

Class properties

sInstance

static private sInstance:SoundManager = null
(read)

Instance properties

mCurrentLevel

private mCurrentLevel:Number = 1
(read)

mMovies

private mMovies:Array
(read)

mSounds

private mSounds:Array
(read)

Class methods

getInstance

static function getInstance (

Returns reference to singleton instance of SoundManager

Instance methods

addExternalSound

function addExternalSound (
inLinkageName:String, inMovieName:String, inUrl:String, inIsStreaming:Boolean, inListener:Object) : Boolean

Add an external sound. If inIsStreaming is set to false, loading of the external sound starts immediately.
Parameters:
inLinkageName:
unique identifier for the sound
inMovieName :
name of previously added movie to associate sound with
inUrl :
url of sound
inIsStreaming:
specify if sound should be loaded entirely before playing (false) or be played streaming (true)
inListener :
Object to be added as listener to SoundClipEvent.ON_LOADED and SoundClipEvent.ON_ERROR events from the SoundClip
Returns:
  • True if the sound was successfully added. This indicates either that a sound with the specified linkage id has already been added, or that the movie with the specified moviename was not found.

    Note that adding an event listener after adding the sound may result in ON_LOADED or ON_ERROR events going missing. If the sound is loaded from the local system, the event can be fired during the addExternalSound call. It is therefore recommended to use the "inListener" property.
Example:
  • On the timeline, add this code:
    com.lostboys.AppController.main(this);

    Create a new class com.lostboys.AppController, and add the following code:
    import org.asapframework.management.sound.SoundClip;
    import org.asapframework.management.sound.SoundClipEvent;
    import org.asapframework.management.sound.SoundManager;
    import org.asapframework.util.debug.Log;
    import org.asapframework.management.movie.LocalController;
    
    class com.lostboys.AppController extends LocalController {
        private static var TEST_SOUND_NAME:String = "test";
        private static var TEST_SOUND_URL:String = "test.mp3";
        private static var TEST2_SOUND_NAME:String = "test2";
        private static var TEST2_SOUND_URL:String = "test2.mp3";
        private static var SOUND_CONTAINER:String = "main";
    
    
        public function AppController (inMC:MovieClip) {
            super(inMC);
    
            // get sound manager
            var sm:SoundManager = SoundManager.getInstance();
    
            // create sound container clip
            var mc:MovieClip = timeline.createEmptyMovieClip("soundContainer", 0);
    
            // add container clip to sound manager
            sm.addMovie(SOUND_CONTAINER, mc);
    
            // try to add the external sounds, non-streaming so it starts loading immediately
            // For the test, make sure one of these sounds exists, and the other doesn't
            sm.addExternalSound(TEST_SOUND_NAME, SOUND_CONTAINER, TEST_SOUND_URL, false, this);
            sm.addExternalSound(TEST2_SOUND_NAME, SOUND_CONTAINER, TEST2_SOUND_URL, false, this);
        }
    
        //	Event handler for SoundClipEvent.ON_LOADED event from external sounds
        private function onSoundClipLoaded (e:SoundClipEvent) : Void {
            SoundManager.getInstance().playSound(e.name);
        }
    
        //	Event handler for SoundClipEvent.ON_ERROR event from external sounds
        private function onSoundClipLoadError (e:SoundClipEvent) : Void {
            Log.error("Could not load sound with url '" + SoundClip(e.target).url + "'", toString());
        }
    
        //	The main entry point from the timeline
        public static function main (inMC:MovieClip) : Void {
            var controller:AppController = new AppController(inMC);
        }
    
        public function toString() : String {
            return ";com.lostboys.AppController";
        }
    }

    This will play the sound that exists, and output an error message to the trace window with the url of the non-existent sound file.

addMovie

function addMovie (
inName:String, inClip:MovieClip) : Boolean

Add a named movieclip to function as container for sounds
Make sure the movieclip resides in the same swf as the sounds that will be added to it.
Parameters:
inName:
(unique) name of movie to be used as identifier
inClip:
the movieclip itself
Returns:
  • True if the movie was successfully added, otherwise false (due to non-unique name).

addSound

function addSound (
inLinkageName:String, inMovieName:String) : Boolean

Add a sound from the library, associated with the specified movie name
Parameters:
inLinkageName:
linkage id of the sound in the library
inMovieName :
name of previously added movieclip in which the sound will be created
Returns:
  • True if the sound was successfully added, or false if not. False indicates either that a sound with the specified linkage id has already been added, that the movie with the specified moviename was not found, or that a sound with the specified linkage id does not exist in the library.

createSoundClip

private function createSoundClip (
inLinkageName:String, inMovieName:String) : SoundClip

Create a new SoundClip object with the specified name in the specified movie
Parameters:
inLinkageName:
unique identifier for the sound clip
inMovieName :
name of the movie in which the sound clip will be created
Returns:
  • The created SoundClip, or null if an error occurred. This indicates that a sound with the specified name has been added already, or that the movie with the specified movie name has not been added.

getMovie

private function getMovie (
inName:String) : SoundMovie

Return the movie with the specified name.
Parameters:
inName:
name of the movie to be found
Returns:
  • The movie, or null if none was found.

getMovieIndex

private function getMovieIndex (
inName:String) : Number

Return the index of the movie with the specified name
Parameters:
inName:
name of the movie to be found
Returns:
  • The index, or -1 if none was found.

getSound

function getSound (
inName:String) : SoundClip

Get the SoundClip object for more direct manipulation.
Parameters:
inName:
name of the sound for which the SoundClip object is requested
Returns:
  • The SoundClip object if found, or null if none was found.

getSoundIndex

private function getSoundIndex (
inName:String) : Number

Return the index of the SoundClip object with the specified name.
Parameters:
inName:
name of the object to be found
Returns:
  • The index of the sound, or -1 if none was found.

muteAllSounds

function muteAllSounds (
) : Void

Set the volume of all sounds to 0. The original volume is retained, and can be reset with unmuteAllSounds.
Note that this function affects only sounds played with the SoundManager.

playSound

function playSound (
inName:String, inPlayCount:Number) : Boolean

Play a sound with the specified name and play count. If the sound was playing, it is stopped.
Parameters:
name :
Sound identifier.
playCount:
Number of times to play the sound. 0: play in a loop; nothing: play once.
Returns:
  • True if successfully started. False indicates that a sound with the specified name has not been added, or that an external sound has not loaded (yet).
Usage:
  • To play a sound once:
    SoundManager.getInstance().playSound("test");

    To play a sound 10 times:
    SoundManager.getInstance().playSound("test", 10);

    To loop a sound:
    SoundManager.getInstance().playSound("test", 0);

    Note: looping is only devoid of glitches with sounds in the library. External sounds are always with a glitch.

    Note: in certain cases, when used with an external sound (an mp3 file), you can pass playSound only after a frame has passed. For example from a controller class:
    private function onSoundClipLoaded (inName:String) : Void
    {	// passed from SoundClip
        mc.onEnterFrame = function() {
            delete mc.onEnterFrame;
                var theSoundManager:SoundManager = SoundManager.getInstance();
            theSoundManager.playSound("mainsound", 0);
            theSoundManager.setSoundVolume("mainsound", 70);
        }
    }
    or you can use the class org.asapframework.util.FrameDelay for this: (example untested!)
    private function onSoundClipLoaded (inName:String) : Void
    {	// passed from SoundClip
        new FrameDelay(SoundManager.getInstance(), playSound, ["mainsound", 0]);
    }

removeMovie

function removeMovie (
inName:String) : Void

Remove a previously added movie with specified name. Also removes all associated sounds.
Parameters:
inName:
(unique) name of a movie

removeSound

function removeSound (
inName:String) : Void

Remove a previously added sound
Parameters:
inName:
(unique) name

removeSoundsInMovie

function removeSoundsInMovie (
inMovieName:String) : Void

Remove all sounds in a previously added sound container movie
Parameters:
inMovieName:
name of movie

setSoundVolume

function setSoundVolume (
inName:String, inVolume:Number) : Boolean

Parameters:
name :
Sound identifier.
volume:
A number from 0 to 100.
Returns:
  • True if successfully applied to sound. False indicates that a sound with the specified name was not added.

stopAllSounds

function stopAllSounds (
) : Void

Stop all sounds.
Note that this function affects only sounds played with the SoundManager.

stopSound

function stopSound (
inName:String) : Boolean

Stop the sound with the specified name
Parameters:
inName:
identifying name of the sound to be stopped
Returns:
  • True if the sound was stopped. False indicates that a sound with the specified name was not added, or that an external sound has not loaded (yet).

toString

function toString (
) : String

unmuteAllSounds

function unmuteAllSounds (
) : Void

Reset the original volume of all sounds.
Note that this function affects only sounds played with the SoundManager.