Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now we'll prepare Mp3 Player to be able to control the volume of the songs. If you're following along, I'm working in 10_Setting_Volume.fla, in the 10 folder in the Chapter 9 folder, in the Exercise Files folder. The first thing we'll do is, do our super fun class path set up by clicking the Publish Settings button, than ActionScript 3.0 Settings button and then we'll delete the current class path, browse to a new one and will choose the Chapter 9 folder and 10, start, classes, and click choose, ok and OK to close all the windows. Then we'll open up the ActionScript files we'll be working with for this movie. Choose File, Open and then navigate to the Chapter 9 folder, in the 10 folder, start, classes, com, lynda, audio, mp3 and then I'm going to select SoundLoader and I'm going to Cmd+click VolumeControl, its Ctrl+click on the PC and click Open and it will open both of those files.
The first thing we need to do is create a soundTransform object that we can use to control the volume of our sounds. So at the bottom of the variable sections in the SoundLoader.as file we'll create a new Private variable and we'll call this variable songVolume. The data type will be SoundTransform, capital S for sound, capital T for transform. We'll give this variable a value inside of the songLoaded function. So scroll down to the songLoaded function. Now we're going to give it a value in two places here. That's because the first time we give it a value when we initialize an instance of this class. So find the conditional statement that checks to see if this class is yet initialized.
At the top of that conditional statement we'll set songVolume= song.soundTransform such it initializes the variable to be whatever the current soundTransform value is of our SongChannel object called song. Then it's basically going to give it the default value and the default volume of one and one is the full volume of sound, sound volume goes from zero to one. The next area that we will work with our songVolume variable inside of this function is right below this conditional statement where we'll create an else statement. If this object is already been initialized then what we'll do is we'll set song.soundTransform equal to songVolume.
That way what the new song loads, the new song will adopt all the properties connected to the soundVolume object. That way when a new song is loaded it adopts the same volume as the last song had. Another area where we need to set the volume of our song is when we play our songs. Let's scroll down and find a playSong function. The reason why we need to do this is because we're giving a new value to our song variable our soundChannel object. If we didn't update the song's soundTransform property here it would go back to its default soundTransform property and have a volume of one, which could kind of be annoying if the user turned the volume down a little bit and then clicked to play the sound and the volume went back up to the max volume. So let's go to the next line, right below song= loader.play and then we'll type song. soundTransform= songVolume.
Now that we've connected our song and our songVolume in the right places, let's create some functions that we can use to manipulate the volume of the song. Scroll down at the very bottom of all the functions, create a new function. This is going to be a public function, so type public function and this is going to be called setVol with a capital V short for set volume and it's going to receive a variable called vol and that's going to be a Number, vol is short from volume. It's not going to return any values and what we're going to do in here is simply set songVolume.volume egual to the value that's passed in as vol.
So songVolume.volume= vol. On the next line we'll connect our new volume to our song, so type song.soundTransform= songVolume So we can use this function to set the volume of our sound against public so we can use it in different class files and with different instances. So let's go down a few lines and we'll create another function. This is also going to be a public function and this function is going be used to get the current volume. So call this getVol with capital V and it's not going to receive any value and we'll simply have it return the current value of songVolume.volume.
You can run this function from anywhere and get the current songVolume. In your code, scroll up, and find the initialized area or find the conditional statement that checks to see if this instance has yet been initialized and it's inside of the songLoaded function and on my code is from line 44 to line 50. At the very bottom of this function we'll give a value to musicPlayer.volControl. Remember that's the volume control instance that we created in the Mp3Player class.
So we'll give that a value of new VolumeControl, capital V and capital C and we're going to pass in a few variable values. So the first one is going to be the Volume Movie Clip and that's going to be musicPlayer.volBar. Then we're also going to pass in our Mp3Player instance, which is musicPlayer. Now that we've created a new instance of the VolumeControl class we can start working with the VolumeControl class.
So choose File, Save. And now we'll go over to VolumeControl.as and start defining what this class does. Inside of the VolumeControl class we'll create a few variables or a few properties and they will both be private. So we'll create a private variable called musicPlayer and that we'll be an instance of the Mp3Player class. Then on the next line, we'll create another private variable called volBar and the data type of that will be MovieClip and this is going to represent the volumeBar MovieClip.
Inside of the parentheses for the VolumeControl constructor function we need to receive those two values that we've passed in from SoundLoader.as. Now the first value there was our volumeBar, so I'm just going to call this vBar for short and the data type of course is going to be MovieClip. There are also going to need to accept the Mp3Player instance so we'll call this player and the data type is going to be Mp3Player, and inside of the constructor function we'll initialise the values of our musicPlayer object and our volBar object.
So type musicPlayer equals player and volBar equals the vBar. On the next line we'll set the buttonMode of our volBar object to true. This will make the Hand Cursor show when you roll over that Movie Clip. As long as the Movie Clip is interactive meaning it has mouseEvent listeners associated with it. On the next line we'll make our volumeBar give us some feedback and show what volume the sound is currently playing at, or what the volume is going to be when we press the Play button.
So type volBar.gotoAndStop and in the parentheses I'm going to pass in Math.round and in the parentheses for math.round I'm going to get the current volume, I'm going to multiply that by the total frames in the volBar Movie Clip. What that's going to do is give me some feedback as to where the volume is. For example the volume is going to be in a value between zero and one. The volBar Movie Clip has 10 frames.
So by multiplying the values if the volume is .5, we multiply that value by the total frames of the volBar Movie Clip. Then we make the volBar go to and stop at frame five. So it plays halfway through its animation. It gives us the visual feedback that the volume is halfway up. So on our Math.round I'm going to type musicPlayer. soundLoader.getVol and it's in parentheses times volBar capital B, dot totalFrames.
And that should do it. So let's save the file and test the movie. File, Save to save the file. Test the movie using Cmd+Return on the Mac or Ctrl+Enter on the PC. Now you can see the Volume Slider is no longer animating, shows the Hand Cursor when I roll over it and is showing me some feedback about the current volume, which is a full volume. We'll close the window. So now I associated the volume of our sound with the volumeBar on the stage. The next step is to add interactivity or we can control the sound by dragging the Sound Bar.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.