Viewers: in countries Watching now:
In ActionScript 3.0 in Flash Professional CS5 Essential Training, Todd Perkins shows Flash designers how to incorporate ActionScript code into their projects and create interactive presentations and applications. The course includes a review of ActionScript language basics and the object-oriented programming (OOP) methodology, a tour of those Flash Professional CS5 features designed for developers, such as code hinting and the Code Snippets panel, and instructions on interacting with objects in the Library and placing code on the Timeline. Exercise files are included with the course.
Most audio players you see provide some feedback about the playback progress of a song. Let's take a look at how to add that functionality to our app. Scroll down to the clickedPlay function. At the bottom of that function, type addEventListener, with a capital E, capital L, and in the parentheses listen for Event.ENTER_FRAME, in all caps. After that, type a comma and a space, and then we will run a function called trackPlayback, capital P. Remember that when you listen for the ENTER_FRAME event, Flash runs that code multiple times per second.
So if possible, you want to disable that code when you are not going to need it. So let's copy this line of code, select it, Command+C or Ctrl+C to copy, and then at the bottom of clickedPause, we will paste that code and change add to remove. That way when the sound is not playing, we won't be listening for the ENTER_FRAME event. So now we will define the trackPlayback function. Scroll to the bottom of your code and create the trackPlayback function. Make sure the playback has a capital P. You will receive an event object with the datatype of Event, with a capital E, colon void for no return data.
Then in the curly braces, we will write the code to display playback progress. The first task is to get the percentage that has been played. So create a variable called perPlayed, with a capital P, datatype it to a number, and set it equal to the current position of the song divided by the total duration of the song. So to get the position, you go through the SoundChannel class, and our SoundChannel instance is called songController, capital C. After that, type a dot, and then type position. So we will divide that by song.length.
So you get the length of the song through the sound class, and our instance of the Sound class is named song, and you get the position of the song through the SoundChannel class, and our instance is called songController. So that will give us the percentage that has been played. Now, we are going to position the slider based on that percentage. Let's type slider.x, space, equals, space. Now, we know we are going to multiply the percent played by something. So just type perPlayed, with a capital P, space, asterisk, and a space. Now, the way we want to multiply it is so that when perPlayed is equal to 0, then the slider is all the way at the left edge of the bar, and that when it's equal to 1, which is when the whole song has been played, the slider goes all the way to the right edge of the bar.
So here, I'll write some parentheses, and then type bar.width - slider.width. Remember, we always subtract the width of the object for this reason. I close the Actions panel and reveal the slider. If I had it equal to bar.width, then the maximum position would be right here at the edge of the bar. But since I am setting it equal to bar.width - slider.width, the right edge of the slider will be aligned with the right edge of the bar at the end.
Now, we need to take this whole value right here that we are going to have, and we need to offset, that because if we don't, the playback progress will look a little bit weird. Just to see an example, type a semicolon at the end of this line and test the movie. Click the Play button, and then watch the slider,. (Music playing.) Notice that it starts at the left edge of the stage; watch where it ends. (Music playing.) There is a little empty space after the end of the song, which is why it's still moving.
So we have a little bit of space. It looks like this has shifted to the left a little bit. Let's take a look at why that's happening. The reason why is because we are just multiplying percentage played by the width of the bar. We are not offsetting the position by the X position of the bar. So if we offset the position by adding a value at the very end, so we'll add on bar.x, then we'll shift that whole movement to the right and the amount of the X position of the bar, so that when percentage played is 0, and 0 times whatever this value is is going to be 0, then the 0 position of the slider is going to be aligned with the left side of the bar.
Let's test the movie, preview the animation. Click the Play button, and watch the slider. (Music playing.) So the slider goes all the way to the end and then stops. So to provide playback progress, you'll have to write the custom code yourself by adding an ENTER_FRAME EventListener.
Inside the ENTER_FRAME event, you can get the percentage played through the SoundChannel's position property and the SoundClass's length property.
Find answers to the most frequently asked questions about ActionScript 3.0 in Flash Professional CS5 Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
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.