Skip to main content
Inspiring
May 25, 2011
Question

Looping sound and changing volume

  • May 25, 2011
  • 2 replies
  • 472 views

What Im looking to do is decrement the volume level on each iteration of a looped sound. How can I achieve this?

here's my code thus far

var playCh:Dictionary = new Dictionary();

//.... private static function loopMySnd(id:String, loopAmount:int):void{   

     playCh[id] = new Sound();   

     playCh[id].addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);   

     playCh[id].play(0, loopAmount);

}

private static function onPlaybackComplete(event:Event):void {            

     trace("The sound has finished playing.");         

}

How do I check each time it loops? I'm currently storing all playing sounds by ID inside a Dictionary object. Keep in mind that there will be different sound playing. I wasn't sure if I should be using a SoundChannel here either.

This topic has been closed for replies.

2 replies

kglad
Community Expert
Community Expert
May 25, 2011

:


var obj:Object={};
var playCh:Dictionary = new Dictionary();
//.... private static function loopMySnd(id:String, loopAmount:int):void{    
     playCh[id] = new Sound();    
     playCh[id].addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);    
     playCh[id].play(0); 
obj[id+"_volume"]=1;
obj[id+"_loops"]=loopAmount;
obj[id+"_origLoops"]=loopAmount;
} private static function onPlaybackComplete(event:Event):void {                  trace("The sound has finished playing.");
var s:Sound=event.target;
var id:String=getKey(s);
obj[id+"_loops"]--;
if(obj[id+"_loops"]>0){
var sc:SoundChannel=s.play(0);
var st:SoundTransform=sc.soundTransform;
obj[id+"_volume"] = obj[id+"_volume"]*obj[id+"_loops"]/obj[id+"_origLoops"];
st.volume = obj[id+"_volume"];
}  }
}

private function getKey(s:Sound):String{
for(var id:String in playCh){
if(playCh[id]==s){
return id;
}
}
return "Error";
}

Participating Frequently
May 25, 2011

Hi,

You need to use channel, ok? there is no other way to decrease sound volume I think. Then you simply need to remember that to change sound volume of your sound/sound channel you have to get reference to sound transform, update and re-assign, see:

var transform:SoundTransform = channel.soundTransform;

transform.volume = newVolumeValue;

channel.soundTransform = transform;

and I think you should create reference to sound channel (when sound starts play(...)) and use listeners added to channel not to sound:

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/media/SoundChannel.html#event:soundComplete

regards,

Peter