A volume slider is a small bar that you can drag to increase or decrease the volume of any sound. In this tutorial, you will learn how to create just that.Let’s create a simple audio volume controller:
step 1) Copy and paste below code at first frame.
step 2) Make a movieClip for sound track named "track_mc".
step 3) Make another movieClie (for volume control) named "slider_mc" inside the "track_mc".
step 4) Insert sound (like .wav n .mp3) at first frame.
// AS 3.0 code for Volume Slider or Audio Control:
var soundVol:SoundTransform = new SoundTransform();
var soundVolume:Number = 1;
// Code that handles the volume slider
var trackBounds:Rectangle = track_mc.getBounds(track_mc);
var xPos:Number = trackBounds.x;
var yPos:Number = trackBounds.y;
var widthPos:Number = trackBounds.width-track_mc.slider_mc.width;
var heightPos:Number = 0;
var bounds:Rectangle = new Rectangle(xPos,yPos,widthPos,heightPos);
track_mc.slider_mc.x = widthPos;
track_mc.mouseEnabled = false;
track_mc.slider_mc.buttonMode = true;
track_mc.slider_mc.addEventListener(MouseEvent.MOUSE_DOWN,dragSlider);
stage.addEventListener(MouseEvent.MOUSE_UP,stopSlider);
function dragSlider(event:MouseEvent):void {
event.target.startDrag(false,bounds);
addEventListener(Event.ENTER_FRAME,setVolume);
}
function stopSlider(event:MouseEvent):void {
track_mc.slider_mc.stopDrag();
removeEventListener(Event.ENTER_FRAME, setVolume);
}
function setVolume(event:Event):void {
soundVolume = track_mc.slider_mc.x/widthPos;
soundVol.volume = soundVolume;
SoundMixer.soundTransform = soundVol;
}
Wednesday, April 7, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment