Home

Awesome

SoundAS - AS3 SoundManager

A modern lightweight AS3 SoundManager for Flash and AIR.

The goal of SoundAS is to simplifying playback of your audio files, with a focus on easily transitioning from one to another, and differentiating between SoundFX and Music Loops.

#Features

#API Overview

Full documentation can be found here: http://treefortress.com/libs/SoundAS/docs/.

###SoundAS This Class is the main interface for the library. It's responsible for loading and controlling all sounds globally.

Loading / Unloading:

Playback:

####SoundInstance Controls playback of individual sounds, allowing you to easily stop, start, resume and set volume or position.

#Code Examples

###Loading

//Load sound from an external file
SoundAS.loadSound("assets/Click.mp3", "click");

//Inject an already loaded Sound instance
SoundAS.addSound("click", clickSound);

###Basic Playback

//Play sound.
    //allowMultiple: Allow multiple overlapping sound instances.
    //allowInterrupt: If this sound is currently playing, start it over.
SoundAS.play("click", volume, startTime, loops, allowMultiple, allowInterrupt);

//Shortcut for typical game fx (no looping, allows for multiple instances)
SoundAS.playFx("click");

//Shortcut for typical game music (loops forever, no multiple instances)
SoundAS.playLoop("music");

//Toggle Mute for all sounds
SoundAS.mute = !SoundAS.mute;

//PauseAll / ResumeAll
SoundAS.pauseAll();
SoundAS.resumeAll();
 
//Toggle Pause on individual song
var sound:SoundInstance = SoundAS.getSound("music");
(sound.isPaused)? sound.resume() : sound.pause();

//Fade Out
SoundAS.getSound("click").fadeTo(0);

//Fade masterVolume out
SoundAS.fadeMasterTo(0);

Groups

//Create a group
var musicGroup:SoundManager = SoundAS.group("music");

//Add sound(s) to group
musicGroup.loadSound("assets/TitleMusic.mp3", "titleMusic");
musicGroup.loadSound("assets/GameMusic.mp3", "gameMusic");

//Use entire SoundAS API on Group:
musicGroup.play("titleMusic")
musicGroup.volume = .5;
musicGroup.mute = muteMusic;
musicGroup.fadeTo(0);
//etc...

//Stop All Groups
for(var i:int = SoundAS.groups.length; i--;){
    SoundAS.groups[i].stopAll();
}

###Advanced

//Mute one sound
SoundsAS.getSound("click").mute = true;

//Fade from .3 to .7 over 3 seconds
SoundAS.getSound("click").fadeFrom(.3, .7, 3000);

//Manage a SoundInstance directly and ignore SoundAS
var sound:SoundInstance = new SoundInstance(mySound, "click");
sound.play(volume);
sound.position = 500; //Set position of sound in milliseconds
sound.volume = .5; 
sound.fadeTo(0);

//String 2 songs together
SoundAS.play(MUSIC1).soundCompleted.addOnce(function(si:SoundInstance){
    SoundAS.playLoop(MUSIC2);
}

//Loop twice, and trigger something when all loops are finished.
SoundAS.play(MUSIC1, 1, 0, 2).soundCompleted.add(function(si:SoundInstance){
    if(si.loopsRemaining == -1){
        trace("Loops completed!");
        si.soundCompleted.removeAll();
    }
}

License

WTFPL