Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now we'll take a look at controlling the playback of our sounds using our Play and Pause buttons. If you're following along I'm working in 08_Play_Pause.fla in the 08 folder, in the Chapter 9 folder, in the Exercise Files folder. The first thing we'll do is our absolute favorite thing in the world and that's Publish Settings, to connect to the appropriate class path and make sure that you're in the appropriate folder, that is Chapter 9, the 08 folder, the Start folder and then the Classes folder.
And then click OK to exit all the windows and now open ActionScript files we'll be working with. Choose File, Open to open some files and we're looking in the Chapter 9 folder, in the 08 folder and then we are going to navigate to start, classes, com, lynda, audio, mp3, and we're going to be working in SoundLoader, and in another class called PlayPauseButton. So I'm Cmd+ clicking the PlayPauseButton class after the SoundLoader class is selected and I'll select both of those classes. You can Ctrl+click to select those applied on the PC. Once you have those selected you can just click the Open button to open both of those ActionScript files. First we're going to work in the SoundLoader class file. In your variable section, below all the other variables you created, I'm on line 17 in my code, lets create a new variable and this one will be another private variable and we'll call this initialise and the data type will be Boolean and we'll set it equal to false. This initialize variable help us out a lot. What we're going to use it for, is we'll create a conditional statement to see if all the other Class files are loaded. And what I mean by that is actually load our sound the very first time we load the first song into our Mp3Player. We're going to run some code that we only want to run once. So we use this initialized variable to make sure that code only does run once. On the next line, let's create a public variable and we'll call that variable resumeTime and will be a Number, and that variable will just represent the time into the song that the playback will resume when you click the Play button.
A lot of times that's going to be at the beginning of the song, but when we use the Pause button, we are going to pause the song in the midddle, so we need to capture the time to continue playing the sound. That's what this variable will do. Next let's scroll down into the soundLoaded function. At the top of this function we'll initialize our resumeTime variable so above all the other code in this function type resumeTime, capital T, equals 0. So every time a news song loads we'll reset that resumeTime variable. Now, at the bottom of this function let's create a conditional statement that checks whether the movie has been initialized. In the parentheses for this conditional statement I'm going to type in an exclamation point and then initialised.
So what we're going to do is say, if the movie is not already initialized, then what we're going to do is run this block of code and then set the initialized variable to true, so this code will only run one time for the first sound that loads. So in the curly braces for the conditional statement we'll set initialized to true and then, on the next line of code, type musicPlayer, capital P, .playPause.
Now remember that playPause property on our music player object refers to an instance of the PlayPauseButton class. Now we haven't yet created that instance of the class, that's what we'll do now. So, what we'll do is we will type equals new and then PlayPause both Ps are capitalized in Play and in Pause and then Button with a capital B and then, in the parentheses, we're going to pass in a few values. We'll pass in the Play button, the Pause button and then the musicPlayer object so that we can control the playback of our sound from our PlayPauseButton class. So the first value we'll pass in is musicPlayer, with capital P, .playBtn with a capital B.
second parameter we're passing in is musicPlayer, with capital P .pauseBtn with capital B and the third parameter we'll pass in is just musicPlayer. Make sure to type a semicolon to end that statement and let's just take a second to look at the code and make sure you've typed everything correctly. Now we're to find some functions that will Play an Pause our sound. Scroll down, below all the other functions that we've already defined, create a new public function, that's because we want to be able to reference this function from our PlayPauseButton class and we'll call this pauseSong with a capital S, we'll have no return value, and here we'll do two things. The first thing we'll do is to set the resumeTime equal to song.position.
When we pause the song we're capturing the songs position so we can play from that position when we click the Play button. On the next line we'll stop the song from playing. So type song.stop. Below this function lets create a public function called playSong. This is also going to have a return data type of void. Inside of this function we'll type song = loader, that's our sound object, loader.play then we'll pass in the parentheses the resumeTime variable.
So when the sound first loads the resumeTime is zero, so the first time you click the Play button the sound will play from the beginning. Once you paused the sound, we'll capture the resumeTime, to capture the current position of the song and we'll stop the sound from playing. Then, when we click the Play button again the sound will play from the resumeTime. Let's save this file by choosing File, Save. Then go over to our PlayPauseButton file. First lets create some variables inside of this file. Above our constructor function, let's create some private variables to represent the values that are passed in when an instance of this class is created. If your remember from when we created a new instance of this class in the soundLoader file earlier in this exercise, we passed in three values to the PlayPause button. Those values were the PlayButton, the PauseButton and the musicPlayer variable, which represents the MP3 Player inserts. So the first variable will be playBtn with a capital B and the data type will be SimpleButton, and the next variable will also be private.
It will be pauseBtn with capital B, and the datatype will also be SimpleButton. We'll set variables or properties the same names in each file. For example in the MP3 Player file and in the SoundLoader file the Play button and the Pause button also have values of playBtn and pauseBtn. These variables can be unique because they're in different classes. So they don't necessarily need to be the same. I've kept them the same here, because that makes them a little bit easier to work with. Let's go to the next line of code and we'll create another private variable, and this one will be called musicPlayer and the data type will be Mp3Player, capital M and a capital P. The constructor function is going to receive three values. The first value is going to be the Play button, and I'm going to call that plButton with capital B and the data type is going to be SimpleButton and the next value is going to be paButton, with capital B and the data type will also be SimpleButton.
Those represent our Play and our Pause buttons and the third value is going to be our musicPlayer. We'll just call that player and also the data type to Mp3Player. Inside of the constructor function, I'm working on line 15 right now, we'll set the values of our variables or properties inside of this function equal to the values passed into our constructor function. So type playBtn with capital B equals plButton with capital B and on the next line type pauseBtn with capital B equals paButton with capital B and on the next line tupe musicPlayer equals player. Next we'll add some EventListeners to our Play and our Pause buttons. So on the next line we type playBtn.addEventListener and event will be MouseEvent.CLICK or run a function called playSong and on the next line we'll type pauseBtn,with a capital B, .addEventListener and the event will be MouseEvent.CLICK and run a function called pauseSong.
On the next line we'll set our Pause Button to not be visible so type pauseBtn.visible= false. what we'll do inside of our playSong and our pauseSong functions, is we'll tog with the visibility of the Play and Pause movie clips, respectively the Play and Pause button, then we'll have those buttons make the song actually play and pause. So let's define those functions right below our constructor function and so we'll type private function playSong that will receive an event with the type MouseEvent and inside of that function what we'll do is set the visible property of the Play button to false we'll set the visible property of the Pause button to true and we'll make our SoundLoader instance play the song. So type musicPlayer, this is referring to our Mp3Player instance .soundLoader, this is referring to our SoundLoader instance, which has all the controls actually play in and pause the songs, .playSong.
Now let's copy this entire function by selecting it and then pressing Cmd+C or Ctrl+C on the keyboard and then right below we'll paste this function using Cmd+V on the Mac or Ctrl+V on the PC. Change the name of the function to pauseSong, show the Play button. So when you click the Pause button then you'll see the Play button and then we want to hide the Pause button. So when you click the Pause button, we hide the Pause button, show the Play button and vice versa. On the next line we'll change musicPlayer.soundLoader.playSong to musicPlayer.soundLoader.pauseSong. Now our Play and Pause button should hide and show as we click them and control the playback of our sound. So let's save this file by choosing File, Save and than we'll test the movie using Cmd+Return on the Mac or Ctrl+Enter on the PC.
Now what we want to do is click the Play button and listen to the sound play and click the Pause button and listen to the sound stop. So the sound plays and stops just fine. Now, the next test is to click the Play button again and to see if the sound plays from the same spot. And the sound is working great. So let's close this window. So now we can use our Play and Pause buttons to control the playback of our sound.
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.