Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now we'll take a look at working with our SoundLoader class to start making the songs load into our Mp3 Player. If you're following along I'm working in 06_Sound_Loader.fla, in the 06 folder, in the Chapter 9 folder, in the Exercise Files folder. We'll start off by connecting this file to our classes so just click the Publish Settings button at the bottom of the Property Inspector, in the Flash tab of the Publish Settings window, click the Settings button by ActionScript version, select the current class path, click the Remove Selected Path button, click the Browse to Path button, find the 06 folder inside of the Chapter 9 folder, choose the Start folder, inside of that folder, and the Classes folder, inside of that folder and then click Choose, click OK and click OK to close the Publish Settings window.
Good times. All right, now let's start working with our Sound Loader class. Choose File, Open and we'll navigate into the Chapter 9 folder, in the 06 folder, Start folder and then Classes, com, lynda, audio, mp3 and we're going to be working with two classes in this exercise, we'll select Mp3Player.as then I'm holding Cmd on my keyboard and that's Ctrl on the PC. I'm going to select the SoundLoader.as file and then I'll click Open to open both of those files.
First let's go to Mp3Player.as. Scroll down all the way to the bottom of the code and below the last line of code that you wrote in the last movie, we're going to create a new instance of the SoundLoader class. If you remember from the last movie, the name of the SoundLoader object was SoundLoader. So below the last code that you wrote type soundLoader, capital L, equals new SoundLoader, so we'll set it equal to a new instance of the SoundLoader class and we're going to pass in two values.
The first value we're going to pass in is our songs array. The next thing we'll pass in is this instance of Mp3Player, so we'll type this. The songs variable represents the paths to all our songs, and we'll need that in order to load the songs. This represents the Mp3 Player object. That way we will be able to comunicate to our MP3 Player from our SoundLoader and as well as from other classes which we'll talk about a little more later on. Once you've written in that line of code, choose File, Save, to save the file and then go over to the SoundLoader.as file.
The first thing we'll do in this file is set up some variables that we're going to be working with. Above the constructor function, I'm on line 11 in my code, I'm going to create some private variables. So create a private variable, we'll call this songList and this is going to represent all of our songs. So it's going to be an Array data type. On the next line, create another private variable called soundReq with the capital R and this is going to represent the URLRequest for the files we are going to load.
On the next line we'll create another private variable and this one is going to be called loader and this is the sound object that will load all of our sounds, so the data type is going to be Sound and we'll set that right here equal to a new instance of the Sound class. As you may know from working with sound that you need a Sound Channel object to control a sound so we'll create a private variable, and we'll call this song and the data type will be SoundChannel.
Lets create another private variable called songIndex, capital I, and this is going to be an integer with an initial value of zero. This will represent the current song, or the current index of our songs array or in this class, the songList Aarray. On the next line, create another private variable called musicPlayer, capital P. This is going to represent the Mp3Player object.
So the data type will be MP3, capital M, then Player with a capital P. Now, when we created an instance of the SoundLoader class in our Mp3Player file, we passed in two values. First our array of songs and then second we passed in the value this representing the instance of the Mp3Player class. So I need to receive those two values inside of our SoundLoader constructor function. So now I'm working in line 18. That's in my code, it may be differing yours, so inside the parentheses put your cursor and type songs and the data type will be Array, so we'll receive that songs array from the Mp3Player instance and we're also going to receive the Mp3Player. So we're going to type player, and the data type is going to be an Mp3Player. Now we'll connect some of the variables that we've created to the objects passed in when an instance of the SoundLoader class is created. So put your cursor in between the curly braces for the SoundLoader function, I'm working on line 20 in my code, and then type songList = songs.
On the next line type musicPlayer = player. So now we've connected our variables inside of our class file to the values passed in when the instance of the SoundLoader class is created. So now this file or this class knows the songs that are passed in when an instance of the Mp3Player class is created. That information from the songs array all the way from our FLA file goes into the Mp3Player file and then into the SoundLoader file, and now we can work with it in here.
On the next line we'll give a value to our SoundRequest variable. Type soundReq with a capital R, and we'll set that equal to a new URLRequest and in the parentheses we'll pass in songList, remember that's our array of songs, and then in square brackets after songList the index of the song that we're going to pass in it's going to be songIndex, which currently is zero.
On the next line of code, we'll make our loader object, which is an instance of the sound class, load the soundRequest file. So type loader.load and we'll pass in soundReq with capital R. So our sound object will load the first song, which if you remember from our FLA file I'm just going to quickly show the ActionScript, is songs navigating to the Songs folder and a song called chad.mp3.
Back in the SoundLoader file, on the next line we'll add an event listener to our Sound object. We'll have it listen for the complete event, once the sound file is finished loading. So type Event.COMPLETE, all caps, and we'll run a function called songLoaded, which we'll define right now. So right below the constructor function, let's create a new private function called songLoaded and it will receive an event with the data type of Event and in curly braces we'll make our song play. So type song, that's our soundChannel object, equals loader.play and make sure to put the parentheses after play.
Now if we've typed everything correctly the sound should play when we test the movie. So let's go to File, Save to save our ActionScript file and then we'll test the movie directly from here using Cmd+Return on the Mac or Ctrl+Enter on the PC. (Music plays.) And the sound plays just fine. Great. Now since we don't want to hear that sound blaring every single time we test the movie let's just make our sound stop right off the bat. So on the next line of code, type song.stop.
We'll control the sound later on when we work with our buttons. Now you may be wandering why we're making the song play and then the song stop right after. well, what happens is, when we make the song play we're actually setting a value to our song object which is actually an instance of the SoundChannel class. By giving our sound a value, even though we're giving it a value and stopping it immediately, we can control the sound while it's stopped. So before you close this file, make sure you choose File, Save to save the file.
So there's a look at loading sounds into Flash which could potentially be familiar to you already. So now that we've loaded our songs into our Mp3Player we can access the ID3 tags inside of those files. We'll take a look of how to do that, next.
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.