Managing Sounds and Music in Actionscript 3

Dealing with sounds in Actionscript can be kind of wacky. Sound Objects, Channels, Transforms…blah. I don’t like messing with all of that, so I’ve built a sweet, easy to use Sound Manager. I don’t suppose it has any magical powers or anything but that’s only because I couldn’t think of anything magical a Sound Manager should do…other than play sounds and control volume.
The Sound Manager Class
It is a Singleton, and can be used from anywhere in your game / project. Simply register all of your sounds to the manager, and play them using IDs rather than having references to the sounds themselves.
Also, I have made it so that you can play a “sound”, or play “music” which is controlled at a different volume. Also, it is set up so that only one music sound will play at a time.
Here is the interface for the Sound Manager:
public class SoundManager { // -- INITIALIZATION --- public static function get instance():SoundManager // Volume Control public function get soundVolume():Number public function set soundVolume( volume:Number ) : void public function get musicVolume():Number public function set musicVolume( volume:Number ) : void public function SoundManager( enforcer:SingletonEnforcer ) public function registerSound( a_sound:Sound, a_id:String ) : void public function playSound( a_id:String, a_startTime:int = 0, a_loops:int = 0 ) : void public function stopSound( a_id:Sound ) : void public function playMusic( a_id:String, a_start:int = 0, a_loops:int=0 ) : void public function stopMusic() : void }
Example Usage
I like that I can use ids to play the sounds, because in my games I tend to use 2-3 sounds for certain effects such as death or explosions, which I like to randomly choose. In the example code you can see how I do this:
public class MainScreen extends AScreen { [Embed(source="../assets/gunshot.mp3")] private var GunshotSound:Class; [Embed(source="../assets/murloc.mp3")] private var MurlocSound:Class; [Embed(source="../assets/diemaggot.mp3")] private var DieMaggotSound:Class; [Embed(source="../assets/mario.mp3")] private var MarioSound:Class; public function MainScreen(a_parentScreen:IScreenItem=null) { super(a_parentScreen); SoundManager.instance.registerSound( new GunshotSound(), "sound0" ); SoundManager.instance.registerSound( new MurlocSound(), "sound1" ); SoundManager.instance.registerSound( new DieMaggotSound(), "sound2" ); SoundManager.instance.registerSound( new MarioSound(), "music" ); var tf:TextField = Utils.createTextField( this ); tf.htmlText = Utils.createHtmlText( "Click the mouse to play 1 of 3 random sounds. \n Press 'spacebar' to play music. \n Press 'up' or 'down' to control sound volume.", "#FFFFFF", 16 ); tf.x = 550/2 - tf.width/2; tf.y = 400/2 - tf.height/2; } public override function update(a_timePassed:int):void { if( Input.instance.isMousePressed ) { var rand:int = Math.random() * 4 - 1; SoundManager.instance.playSound( "sound" + rand ); } if( Input.instance.isKeyPressed( KeyCode.SPACEBAR ) ) { SoundManager.instance.playMusic( "music" ); } } }
As always, download the source at the Google project page.
Here it is in action.

(5 votes, average: 4.80 out of 5)




Brian Yamabe said
am December 20 2008 @ 10:09 pm
This series is great. Do you have a full template project, or is that coming in a future post? What types have games have you created with this framework?
Great job,
Brian Yamabe
Colby Cheeze said
am December 24 2008 @ 10:05 pm
I’ll be doing some open-source games in the future that will show off the structure of a game in a real world setting. For now though, I have a few more components and what not to discuss.
Cloud_9ine said
am February 26 2009 @ 8:10 pm
Dammit! You jsut had to release this, I was going to do this :/. At least Mine is a bit more featureful. Want to see it?
Cloud_9ine said
am February 26 2009 @ 8:13 pm
Also your attmpt at the mario music with your(?) voice sucked.
Colby Cheeze said
am February 26 2009 @ 8:17 pm
Hehe I agree
I didn’t feel like putting a real song in there for the example.
Cloud_9ine said
am February 26 2009 @ 8:18 pm
ok found the link to my engine, many features
http://www.kongregate.com/forums/4/topics/29053
Yu-Chung Chen said
am June 14 2009 @ 7:02 am
Great class, helping me out a lot.
One small thing:
Argument type of stopSound should be String, no?
Brian Busche said
am February 9 2010 @ 1:44 pm
I downloaded the source and published it in Flash CS4. A small change was required in the the ManagingSound.as file in order to get it to compile from the IDE. I import the Main class, and remove the getDefinitionByName in onRootLoaderComplete()
[code]
private function onRootLoaderComplete ( event:Event ):void
{
//Do some cleanup
removeChild(m_infoField);
graphics.clear();
loaderInfo.removeEventListener( Event.INIT, onRootLoaderInit );
loaderInfo.removeEventListener( ProgressEvent.PROGRESS, onRootLoaderProgress );
loaderInfo.removeEventListener( Event.COMPLETE, onRootLoaderComplete );
//go to the next frame and load up the main class
nextFrame();
var app:Object = new Main(stage);
addChild(app as DisplayObject);
}
[/code]
Thanks for the example. This is a very useful Sound class for folk who like implementing Singletons.
6 Very Useful ActionScript Classes About Sound - Ntt.cc said
am February 10 2010 @ 9:27 am
[...] Sound Manager Class [...]