OK, I am trying to mute a sound elegantly by fading the mute out. But when I play it, it restarts full volume, and then plays where it left off at like half volume... Can someone try looking at this?
Code:
var soundon:Boolean = true;
var pausePosition:int;
var s:Sound;
var tf:SoundTransform = new SoundTransform(1, 0);
var curFile:String;
function loadSound(incFile:String){
curFile=incFile;
s = new Sound();
var req:URLRequest = new URLRequest(curFile);
s.load(req);
s.addEventListener(ProgressEvent.PROGRESS, onSoundProgress);
}
var channel:SoundChannel;
function onSoundProgress(event:ProgressEvent):void{
var loadedPct:uint = Math.round(100 * (event.bytesLoaded / event.bytesTotal));
if(loadedPct>15){
channel=s.play();
channel.soundTransform=tf;
channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
s.removeEventListener(ProgressEvent.PROGRESS, onSoundProgress);
}
}
var myTween:Tween=new Tween(tf, "volume", None.easeNone, 1, 1, 1, false);
function changeVolume(evt) {
channel.soundTransform=tf;
}
function completeMute(evt) {
if(soundon)
channel.stop();
trace("COMPLETE:"+tf.volume);
}
this.addEventListener(MouseEvent.CLICK, changeMute);
function changeMute(e:Event){
if(soundon){
soundon=false;
pausePosition = channel.position;
trace("STOP:"+pausePosition);
channel.stop();
gotoAndStop(2);
myTween.continueTo(0, 40);
myTween.addEventListener(TweenEvent.MOTION_CHANGE, changeVolume);
myTween.addEventListener(TweenEvent.MOTION_FINISH, completeMute);
}else{
soundon=true;
gotoAndStop(1);
trace("Play:"+pausePosition);
channel = s.play(pausePosition);
myTween.continueTo(2, 40);
myTween.addEventListener(TweenEvent.MOTION_CHANGE, changeVolume);
}
}
loadSound("one.mp3");
Ahh, stupid mistake.. I had it play frame 1 every pause, which then loads a new sound file.