Imajine Blog Imajine Articles
Contact Me

Flash 8 actionscript tutorial to set volume of externally loaded mp3 file

Availability: ActionScript 1.0; Flash Player 5
Returns : Number - An integer.

In this tutorial, we will learn how to adjust volume of an externally loaded MP3 file in flash. As we all know, to get this done, we are basically revolving around sound object. The method used to adjust sound volume is setVolume() of sound object. Similarly we can even retrieve the already set volume by getVolume() method, getVolume() returns the sound volume level as an integer from 0 to 100, where 0 is off and 100 is full volume. The default setting is 100.

To begin with the tutorial, we will need to load external MP3 file, which is already covered in our previous tutorial - Load external MP3 file. For those who have not gone through the above tutorial please read through and then return back to this tutorial.

Now we need to create a volume control horizontal slider and add script to it. Follow the steps given below to create slider movieclip.

  • Create a empty movieclip called slider.
  • Open this slider movieclip and draw an horizontal line using pencil tool from your tool bar, probably a horizontal line of 150px width.
  • Select this line and press F8 to convert this to a movieclip symbol, name it line, select movieclip option and press OK.
  • Now draw a rectangular vector similar to the orange slider seen in the above .swf file of volume controller.
  • Select this rectangle and press F8 to to convert this to a movieclip symbol, name it dragger, select movieclip option and press OK.
  • Give and instance name to this dragger movieclip - "scroller".
  • Right above this dragger movieclip, insert a dynamic text area, with this text area still selected, type "per_txt" in the variable text area of property panel. Then convert this dynamic text area to a movieclip, name it "per_mc" and give this movieclip an instance name, "per_mc"
  • Add another layer to the slider movieclip and name it "actions". Select the keyframe in "actions" layer, then in actions panel, insert below script. If you can not see actions panel, press F9.

    ratio=100;
    per_mc.per_txt = ratio + "%";
    _root.mySound.setVolume(ratio);
    scroller.onPress = function() {
    this.startDrag(true, 0, 0, line._width, 0);
    this.onEnterFrame = function() {
    ratio = Math.round(this._x*100/line._width);
    per_mc._x = this._x;
    per_mc.per_txt = ratio + "%";
    };
    };
    scroller.onRelease = scroller.onReleaseOutside = function()
    {
    stopDrag();
    _root.mySound.setVolume(ratio);
    per_mc.per_txt = ratio + "%";
    };

Your volume controller is now READY. Press Ctrl + Enter to see what an amasing thing you learnt today. :)

Explanation of the above code -

  • First 3 lines, we set variables and its values.
  • Variable ratio is used to set volume based on the scroller movement and also update the dynamic text value, which is used to determine volume level.
  • Next we need to declare a function for scroller.onPress event. Upon onPress event, we need to enable the global startDrag() function to the dragger movieclip.
  • Functionality of startDrag() function -
    startDrag(target:Object, [lock:Boolean, left:Number, top:Number, right:Number, bottom:Number]) : Void

    target:Object - The target path of the movie clip to drag.

    lock:Boolean [optional] - A Boolean value specifying whether the draggable movie clip is locked to the center of the mouse position (true ) or locked to the point where the user first clicked the movie clip (false ).

    left,top,right,bottom:Number [optional] - Values relative to the coordinates of the movie clip's parent that specify a constraint rectangle for the movie clip.

  • When scroller gets drags, based on its position, we will need to indicate volume level in the dynamic text field area. As dragging can incure randomly, at any posible time, we shall write functionality to check scroller position under onEnterFrame event.
  • Only upon releasing the scroller, we will set volume, which gets covered under the function for scroller.onRelease, scroller.onReleaseOutside.

I hope you enjoyed the tutorial !.

Download the source code.

Tags: Volume control in flash, Tutorial to control volume in flash, setVolume(), getVolume(), Load external MP3 file into flash, Stream MP3 File, play mp3 file, Sound Object in Flash, Music play and stop tutorial in flash

Download Free Photo Gallery

Download Free Photo Gallery

You can download Imajine's Scrapbook Photo Gallery absolutely free of cost and to customize , you need no knowledge of Adobe Flash.  Click here to view Scrapbook Photo Gallery / download

Tutorials
Flash Tutorials
Horizontal 3D Carousel
MP3 Player
Scrapbook Photo Gallery
Text Enhancement
Color object
Glow effect using actionscript
Drawing with actionscript
Blur Effect
Load external .mp3 file
Preload external .mp3 file
Sound Controller
Set volume of externally .mp3 file
View MP3 song details
Actionscript blur effect
Actionscript fade in / out effect
Fireworks Tutorials
Custom Stroke
Typography / Ink blot effect
Photograph focus effect
Rainbow Pattern
Inset Text Effect
Fuzzy Light Blending Mode
Little bulb effect
Old paper effect
'Post it' effect
Hand drawn designs
Black/White and Sepia tone
Water color painting effect
Vintage effect on vector graphics
Solid color, black/white
Photoshop Tutorials
Photo editing
Purple Haze

Portfolio

Logos
Website design
CMS Customization
Flash
Web development
Print


Download free textures and patterns and use it for your personal use.

Click here to download Textures and Patterns.

Download free birthday cards

Free Birthday Cards
Click here to view Imajine's free downloadable birthday cards for personal and corporate use. Imajine's birthday cards are in jpeg format so it is faster to download and send in your wishes to your friends and family.
Bookmark and Share Tell a Friend jayanthi.varma@imajine.in  |  © 2008 Imajine