private var sound:Sound;
private var channel:SoundChannel;
private const INCREMENT:Number = 0.2;//change it as you like
sound = new Sound();
sound.addEventListener(Event.COMPLETE, onLoad);
sound.load(new URLRequest("song.mp3"));
function onLoad(e:Event):void
{
channel = sound.play();
if(stage)
{
stage.addEventListener(KeyboardEvent.KEY_UP, onKey);
}
else
trace("call this from a display object on stage");
}
function onKey(e:KeyboardEvent):void
{
var tr:SoundTransform = channel.soundTransform;
var vol:Number = tr.volume;
if(e.keyCode == Keyboard.UP)
vol += INCREMENT;
else if(e.keyCode == Keyboard.DOWN)
vol -= INCREMENT;
if(vol < 0)//volume ranges from 0 to 1
vol = 0;
if(vol > 1)
vol = 1;
tr.volume = vol;
channel.soundTransform = tr;
}
Run Code Online (Sandbox Code Playgroud)