Written by: Billy T.
Time: 20 minutes
Difficulty Level: Beginner
Requirements: Flash MX
Topics Covered: Creating basic sound controls for an attached sound.
Assumed knowledge: Variables, Paths, Objects, Methods, Flash MX Event Model.

OK I thought I would show you how to control a sound with actionscript.

Here's what you will me making. You can download the source file for this tute here.

OK start by loading Flash MX. Double click on the fps to open the Document Properties window - change the settings to these -

Click OK. Now make 2 new layers so that you have three in total. Name them "actions", "slider" and "buttons".

Select frame 1 of the slider layer and draw a 5px X 70px rectangle - I chose to have no stroke and a fill colour of #999999. Select your rectangle and align it so it's centered horizontally and just a little bit up from the bottom of the stage. Hit F8 to convert it to a movieclip. Name it "sliderMC" and make the registration point is set to the bottom-center.

Click OK. Your new MC should be selected on the stage

Give it an instance name of "slider". Save your file as soundslider.fla into a folder called whatever you want. Now double click on the new mc on the stage to go inside it. Make a new layer and draw a new rectangle - 25px X 5px. Select your new rectangle and look at the align panel. Make sure that to stage is selected and center it vertically and horizontally. Now convert it to a movieclip - name it slideBarMC and set the registration point to the center.

Click OK and give it an instance name of "slideBar".

OK go back to the main timeline. We'll set up the last couple of graphics before we get into the scripting. Select frame 1 of the buttons layer and (up the top of the stage) draw a square and triangle to use as the stop and play buttons. Select the square, convert it to a button named "stopBut" and give it an instance name of stopB. Select the triangle, convert it to a button named "playBut" and give it an instance name of playB.

OK now get yourself a short audio loop (can be an mp3, wav or aiff). In flash, select File/Import to library and locate your sound. Import your sound then click on it once in the library to select it. Open the drop down menu up the top-right of the library and choose "linkage". Set the linkage up like so -

OK let's get into the scripting. Select frame 1 of the actions layer and add these actions -

bgSound = new Sound(this);
bgSound.attachSound("sound1");
bgSound.start(0, 99);
playB.enabled=false;
slider.slideBar._y = -50;
slider.slideBar.onEnterFrame = function() {
        bgSound.setVolume(0-this._y);
};
slider.slideBar.onPress = function() {
        startDrag(this, false, this._x, -65, this._x, 0);
};
slider.slideBar.onRelease = slider.slideBar.onReleaseOutside=function () {
        stopDrag();
};
stopB.onRelease = function() {
        bgSound.stop();
        playB.enabled=true;
        stopB.enabled=false;
};
playB.onRelease = function() {
        bgSound.start(0, 99);
        playB.enabled=false;
        stopB.enabled=true;
};
stop();

Let's go through that bit by bit.

Line 1 - we make a new sound object on "this" timeline. The "this" is very important because if you do not sprecify a target here then flash will assume we want to make the new object on level0 which would cause us problems later on if we wanted to load our sound slider into another movie.

Line 2 - We attach the sound in our library to our new sound object. Remember that "sound1" was the linkage name that we gave our sound.

Line 3 - We start out sound object. The "0" represents the number of milliseconds from the start of the sound that we want flash to start playing - 99.9% of the time you will set this 0. The "99" represents the number of times we want flash to loop our sound.

Line 4 - Turn off the play button (we dont need it at the moment because the sound is already playing).

Line 5 - Set the starting y position of our slideBar

Lines 6-8 - This tells Flash to set the volume of our sound object to whatever the y position of our slideBar is. Now because we are getting the y value of the slideBar from within the sliderMC, we need to convert the negative y value into a positive number to set the volume to. We do this with "0-this._y". So if the slideBar was at a y position of minus 43 then we would set the volume of our sound object to 0 - -43 which = 43.

Lines 9-14 - This sets up the dragging action for our slideBar and constrains it to a rectangle.

Lines 15-19 - Our stop button action. First line stops the sound object. Second line turns on our play button. Third line turns off the stop button.

Lines 20-24 - Our play button action. First line plays the sound object. Second line turns off our play button. Third line turns on the stop button.

OK save your fla and then test it.

OK I think that's about it.

Do not, under any circumstances (ie even if your assignment was due yesterday or your boss is looking to down-size or you're making a site for your mum and it's her birthday tomorrow...), email with questions regarding this tute - that's what the forums are for!

Cheers