ActionScript.org Flash, Flex and ActionScript Resources - http://www.actionscript.org/resources
Controlling Multi-channel Sound using ActionScript
http://www.actionscript.org/resources/articles/203/1/Controlling-Multi-channel-Sound-using-ActionScript/Page1.html
Won Dong
This user is yet to take control of their account and provide a biography. If you are the author of this article, please contact us via support AT actionscript DOT org. 
By Won Dong
Published on June 3, 2006
 
Written by: Won Dong
E-mail: [email:[email protected]]
URL: http://www.hirihiri.com/blog
Difficulty Level: advanced
Requirements: Flash MX 2004
Assumed Knowledge: OOP, Class, Sound Object

ActionScript Sound Control Classes
Written by: Won Dong
E-mail: [email:[email protected]]
URL: http://www.hirihiri.com/blog
Difficulty Level: advanced
Requirements: Flash MX 2004
Assumed Knowledge: OOP, Class, Sound Object

Download the code

Do you know that Macromedia Flash only suppport maximum 8 channel sounds? For instance, if your current sound channel reachs maximum limit, Flash cannot play new sound unless one of the play sound ends. So, I made SoundController class, which manages sound channel. When current channel get to maximum sound limit, SoundController class stop the oldest sound, thus you can play the new sound. The mechanism of this class is similar to "Queue" structure. When you add a new sound, the oldest sound pops.

Here is a structure of the class:
  1. soundcontroller.maxSound : Limit of flash. Fixed value. Default is 8.
  2. soundcontroller.BindInst : Interface with instrument.
  3. soundcontroller.soundQueue : Queue structured Array. Playing sounds are stored in this array.
  4. soundcontroller.Play(soundID) : Play a sound. And manage sound queue automatically.

Here is the full source:

[as]/* Copyright 2005 Won Dong All rights reserved.
http://www.hirihiri.com/blog
*/
import hirihiri2.meloboard.instrument;

class hirihiri2.meloboard.soundcontroller {
        var maxSound:Number = 8;
        public var BindInst:instrument;
        public var soundQueue:Array = new Array();
        function SetInst(InstName:instrument) {
                BindInst = InstName;
        }
        function Play(sndPitch:Number) {
                var i;
                this.RefreshQueue();
                for (i=0;i<this.soundQueue.length;i++) {
                        if (Number(this.soundQueue[i])==sndPitch) {
                                this.BindInst.InstArray[Number(this.soundQueue.splice(i))].stop();
                        }
                }
                this.soundQueue.push(sndPitch);
                while (this.soundQueue.length>this.maxSound) {
                        this.BindInst.InstArray[Number(this.soundQueue.shift())].stop();
                }
                this.BindInst.InstArray[sndPitch].start(0);
        }
        function RefreshQueue() {
                for (var i=0;i<this.soundQueue.length;i++) {
                        if (this.BindInst.InstArray[Number(this.soundQueue[i])].position==this.BindInst.InstArray[Number(this.soundQueue[i])].duration) this.soundQueue.splice(i);
                }
        }
}[/as]

[as]/* Copyright 2005 Won Dong All rights reserved.
http://www.hirihiri.com/blog
*/
class hirihiri2.meloboard.instrument {
        private var InstName:String;
        public var InstArray:Array = new Array();
        public var MinPitch:Number;
        public var MaxPitch:Number;
        public var Base:String;
        public var LoadProgress:Number = 0;
        public function onProgress(Percent:Number) {};
        public function onLoad() {};
        function instrument(InstFileName:String) {
                this.LoadInst(InstFileName);
        }
        function LoadInst(URL:String) {
                var InstXML:XML= new XML();
                var owner:instrument = this;
                InstXML.load(URL);
                InstXML.onLoad = function(success:Boolean){
                        if (success) {
                                owner.LoadProgress=0;
                                owner.onProgress(0);
                                owner.InstName = InstXML.firstChild.attributes.name;
                                owner.MinPitch = Number(InstXML.firstChild.attributes.minpitch);
                                owner.MaxPitch = Number(InstXML.firstChild.attributes.maxpitch);
                                if (InstXML.firstChild.attributes.base!=undefined) {
                                        owner.Base = InstXML.firstChild.attributes.base;
                                } else {
                                        owner.Base = ' ';
                                }
                                for(var i=owner.MinPitch;i<=owner.MaxPitch;i++) {
                                        //                    trace("Loading..."+owner.Base + owner.InstName+i+".mp3");
                                        owner.InstArray[i]= new Sound();
                                        owner.InstArray[i].loadSound(owner.Base + owner.InstName+i+".mp3",false);
                                        owner.InstArray[i].onLoad = function(success:Boolean) {
                                                if (success) {
                                                        owner.LoadProgress++;
                                                        owner.onProgress(Math.floor(owner.LoadProgress*100/(owner.MaxPitch-owner.MinPitch+1)));
                                                }
                                        }
                                }
                        } else {
                                trace("connectionFailed");
                        }
                };
        }
}[/as]