Search Tutorials
Introduction The
sound object is a very important aspect of Flash. Dynamically
controlled sounds played directly from the library can only
be realized by using this versatile object. Check out this
tutorial and get a great start on using the sound object
in your movies! And of course, let
us know if you have any questions or comments.
Data Fields
Included Files:
I - Setting up the Flash Movie
3. First we will fill in our text layer, so select it. For the
tutorial you will need one dynamic text field, so drag out a small text field using the
Text Tool [T]. In the Text Options panel, make sure to select "Dynamic Text"
and "Single Line" from the drop down menus. In the Variable field, fill in something
useful like "volumeText". As far as placement is concerned, you can use the tutorial
file as a template if you want, but location should coincide with your specific application. ![]() 6.
There is one additional step we need to take before adding code to our movie, and
that's importing and setting up our sound file. I used a cool .wav file we had laying
around the office, but Flash supports several types including mp3. Select File> Import [Ctrl + R] and navigate to any sound file, or the one
we provide with the tutorial files. Now select the file in your library,
and right click on it. Select Linkage… from the list. Type in an appropriate
identifier like soundFile and select "Export this Symbol" before
hitting OK. This procedure makes the sound file available to access from the
movie dynamically using Actionscript. II - Entering Code
var volPercent = 50; volumeText = volPercent + "%"; volumeFill._yscale = 50; The first 3 lines simply initiate the variables and setup the movie. volPercent tracks the volume level and goes from 0 to 100, to make the math easier. volumeText is a text field that uses volPercent to create the text field output for formatted display to the user. volumeFill is the instance name of our volumeFill library item. By setting its _yscale property to 50, we set it to be half of its full y scale when our movie begins. The next 3 lines create the sound object and assign it to the variable mySound. mySound = new Sound(); first creates the sound object with the new object constructor and then attaches it to mySound so we can use it. In the next line we use the sound object's attachSound method to directly attach a sound file from the library to our object so we can control it with Actionscript. We send it "soundFile" as an argument because that's the name of our externally linked sound file library member. Last, the setVolume method initially sets the volume of mySound to volPercent * 2, or 100. There is no maximum to the value you can use with setVolume, but I have found that
0 - 200 is a good range, and that's the reason behind always multiplying by 2, to get
a number in the 0 - 200 range. 8. With the main block of code out of the way we can start attaching code to the buttons. Select the Play button on the stage and add the following code to the Object Actions panel:
on (release){
mySound.stop();
mySound.start(0, 10000);
}
First, we wrap everything in an on(release) button handler, specifying that the
code within should be played whenever the user releases this button. The stop method
may seem a little out of place here, but if you don't use it you can quickly build up
multiple copies of the same file playing on top of each other. This could be an effect
you're after, and if so, take this method out. We call the start method of our object,
designating the sound to start at 0 seconds into the file [the sound offset],
and loop 10000 times [there is no infinite setting so if you want it to loop infinitely
like I did just set it high]. Now let's add code to the stop button:
on (release){
mySound.stop();
}
Well, that's about as easy as it gets. The stop method stops the file on release of this
button. And on the up volume button:
on (release){
if (volPercent != 100){
volumeFill._yscale += 2;
volPercent += 2;
volumeText = volPercent + "%";
mySound.setVolume(volPercent * 2);
}
}
We wrap this code in an if statement to check and see whether or not our volume is maxed
out. If it is [volPercent = 100], there's no need to add more. On every release
of the volume buttons, I decided that I want my volume to raise or lower 2 percentage
points. So that's where the 2's come from. If you want it to raise and lower 5
instead, just replace the +=2's with +=5's and you're good to go. Just
for the newer scripters out there, if you take a variable and add 2
to it, you can write it as variable = variable + 2, or variable
+= 2. It's the exact same. So, on each release we do the following. First, scale
the volumeFill movieclip up 2 percent to show our user graphically
where the volume is at. Second, increase our variable volPercent by 2
to keep track of where the volume is at. Third, update our text field for the user, and
fourth, reset the volume of our sound file using our new value. Now finally the down
volume button:
on (release){
if (volPercent != 0){
volumeFill._yscale -= 2;
volPercent -= 2;
volumeText = volPercent + "%";
mySound.setVolume(volPercent * 2);
}
}
You can see that the button code is identical for the most part. The main difference is
that in our if conditional we check to see if volPercent is 0 instead of
100, because we are checking the lower limit this time. Additionally, we subtract
2 from our values to lower the volume instead of raising it. If you haven't done
so already, check out the files and try it yourself! And come
to our site to find more great tutorials by Programming Art!
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||