/* SoundFader class 0.3a 2006-08-09 comfortably fade sounds in and out, loop and get a bit control without having these huge amounts of nasty code in your main project file. usage: sf=new SoundFader; sf.addSound('mysound.mp3',true); sf.fadeSound(startVol,endVol,fadeTime); and there you are. some nice eventhandlers, like onFadeDone or onSoundAdded, included note: start and stop are fading, too - complete smooth sound operating */ class SoundFader{ private var currentVolume:Number; var controllerClip:MovieClip; private var isStreaming:Boolean; private var isPlaying:Boolean; private var theSound:Sound; private var destVolume:Number; private var _setVolIntvl:Number; public var isLoopSound:Boolean; private var actualPosition:Number; public var stopTime:Number; //fading time public var globalFadeTime:Number; //maybe not needed, uses stopTime public var startPercentage:Number; //use to define a non-streaming-sounds has-to-be-loaded percentage before playback public var loadPercentage:Number; //may be useful for a preloader or something public var startStopVolume:Number; //what is this exactly for? public var startTrim:Number; //loop trim time at the beginning in ms public var endTrim:Number; //same for the end of the loop private var Owner:Object; //timeline private var pOnFade:Function; //callback function private var pOnSoundAdded:Function; //callback function //prototype - constructor function SoundFader(){ Owner=arguments[0]; stopTime=1000; startStopVolume=50; } //callback function, is called when fading is completed public function set onFadeDone(f:Function){ pOnFade=f; } //kind of onLoad-handler, see addSound for details public function set onSoundAdded(f:Function){ pOnSoundAdded=f; } //add a sound file function addSound(url:String,streaming:Boolean){ this.theSound=new Sound(); this.isStreaming=streaming; if(streaming){ isPlaying=true; } this.theSound.loadSound(url,streaming); //maybe using intervals later on var tmpSoundMc:MovieClip=this.Owner.createEmptyMovieClip('_watchSoundLoaded',this.Owner.getNextHighestDepth); //scope var on object tmpSoundMc.soundFaderObj=this; tmpSoundMc.onEnterFrame=function(){ var sfo=this.soundFaderObj; this.tsLoaded=sfo.theSound.getBytesLoaded(); this.tsTotal=sfo.theSound.getBytesTotal(); sfo.loadPercentage=Math.round(this.tsLoaded/this.tsTotal*100); if(this.tsLoaded>50){ if((this.tsLoaded==this.tsTotal)||(sfo.loadPercentage==sfo.startPercentage)){ sfo.pOnSoundAdded(); //fires if sound is loaded or has reached a certain percentage delete this.onEnterFrame; delete(this); } } } this.theSound.setVolume(0); this.currentVolume=0; } //increase/decrease volume private function mSetVolume(){ this.currentVolume=this.currentVolume+1; this.theSound.setVolume(this.currentVolume); trace(this.currentVolume+' '+theSound.position); if(this.currentVolume>this.destVolume){ clearInterval(_setVolIntvl); pOnFade(); } } private function nSetVolume(){ this.currentVolume=this.currentVolume-1; this.theSound.setVolume(this.currentVolume); if(this.currentVolume=(theSound.duration-endTrim)){ theSound.stop(); theSound.start(startTrim,1); theSound.setVolume(this.currentVolume); } } function startSoundRaw(where){ var _complIntv=setInterval(this,"watchComplete",2); //every TWO MILLISECONDS the Sound.position is watched theSound.start(where,1); this.isPlaying=true; } //encapsulation galore //nothing but Sound.position function getPos(){ return theSound.position; } //same here for volume function getVol(){ return theSound.getVolume(); } }