|
|
Sometimes you'll want to take control of the player out of the hands of the user and manage the player through ActionScript. For example, if you are using the audio player for background music on your site, and you want the music to turn off when the user gets to a certain area of your site, what are you to do? Trust the user to turn it off and concentrate on an important part of your site's experience? Not likely. This is why certain controls have been added to both the audio and video players to give you the last word in controlling them.
On the CD | We will use the audio player as an example by using the player we created earlier, but this time we'll add some outside buttons that will play, stop, and pause the player. An exact copy of the AudioPlayer.fla has been created and named AS_Controller_AudioPlayer.fla, which can be found in the Chapter 8 folder on the CD. With this file, you will start by giving your audio player the instance name audio_player. To do this, click the audio player and press Command/Ctrl+F3 to open the Properties panel. The Component text field currently says < Instance Name >. Click in the text field to clear that out and type audio_player, as shown in Figure 8.21. |
Now, create three buttons that will be used to control the audio in the player and arranging them along the bottom of the audio player, as you can see in Figure 8.22.
Figure 8.22: Create play, stop, and pause buttons that will be used to control the audio player.
Now we must add actions to the buttons that will control the audio player. Although you might recoil from just the mention of ActionScript, it is actually quite uncomplicated. You are familiar with the basic functions that are attached to every movie clip such as stop (), play (), and gotoAndStop (frame). What it means for these functions to be attached to the movie clip is that if you have a movie clip with the instance name my_movie, then you can use the following ActionScript to play the contents in the movie: my_movie.play (). This same structure is preserved in the AudioPlayer component—meaning, if you want to stop the audio in the player, you only have to write the ActionScript: audio_player.stop (). In a sense, you are pressing the Stop button with ActionScript. There are other functions you are can use too; some of the most basic are described in Table 8.1.
![]() |
FUNCTION NAME | DESCRIPTION |
---|---|
| |
stop | Stops the audio from playing and resets the playhead to the beginning of the song. |
play | Plays the current song if the player is not already. |
pause | Stops the audio from playing, but the playhead stays still. |
next_track | Plays the next track in the playlist. |
previous_track | Plays the previous track in the playlist. |
![]() |
Bearing in mind all this new information, we can now write the ActionScript for the buttons we created (those scripts are shown in Listings 8.1, 8.2, and 8.3). To apply ActionScript to a button, click the button that resides on the stage and press F9. A blank text window will appear where you can type your ActionScript. The first part of the code specifies what the user needs to do to execute the button. Since we want the button's actions to be executed when the user presses the button, we wrap all of its actions with on (press) (see Figure 8.23). The action in the press button event is simply a call to one of the functions listed in Table 8.1, which is attached to the component.
Listing 8.1: Code for the Play Button
![]() |
on (press) { audio_player.play (); }
![]() |
Listing 8.2: Code for the Stop Button
![]() |
on (press) { audio_player.stop (); }
![]() |
Listing 8.3: Code for the Pause Button
![]() |
on (press) { audio_player.pause (); }
![]() |
Figure 8.23: Actions placed on the external Play button
Now test the movie and see how you can control the audio player with buttons that are completely separate from the player. Although the ActionScript in this sample was placed on buttons, it can be placed anywhere, such as frame actions. Remember this to use the component to its full potential.
The video player also has similar functions attached to it (see Table 8.2). You can use them in the exact same way as in the sample of the audio player.
![]() |
FUNCTION NAME | DESCRIPTION |
---|---|
| |
stop | Stops the audio from playing and resets the playhead to the beginning of the song. |
play | Plays the current song if the player is not already. |
pause | Stops the audio from playing, but the playhead stays still. |
next_track | Plays the next track in the playlist. |
previous_track | Plays the previous track in the playlist. |
fast_forward | Starts the video fast-forwarding. |
rewind | Starts the video rewinding |
stop_fast_forward | Stops the video from fast-forwarding, if it is indeed fast-forwarding. Keep in mind that the player will continue to fast-forward until this function is called. |
stop_rewind | Stops the video from rewinding, if it is indeed rewinding. Keep in mind that the player will continue to rewind until this function is called. |
![]() |
|
|