Behavior 6: Toggle Play/Pause
Behavior 6: Toggle Play/Pause
The Toggle Play/Pause behavior allows you to add code to a button so that it toggles between play and pause when a video is running in the component. The following instructions show you how to add this behavior to a custom button.
To use this behavior:
- Add a Media Playback component to the Stage.
- Add a button to the Stage and select it.
-
Open the Behaviors panel and click the plus (+) icon to access the list of behavior categories. Select the Media Controls category and choose Toggle Play/Pause from the list. The behavior’s dialog box opens (see Figure 6).
- Browse for the media display instance in the target list in the bottom portion of the dialog box. This targets the instance name of the component displaying the video. A best practice is to use the Relative option for the path, as shown above. If you have problems adding paths to component buttons, try using the Absolute option instead.
- Click OK.
Here’s the code that is added to the button:
// Start Toggle Play/Pause Behavior
var m = myMediaControl6;
if(!m.playing)
{
if(Math.round(m.playheadTime) == m.totalTime)
m.stop();
m.play();
}
else{
m.pause();
}
// End Toggle Play/Pause Behavior
Tip:
You can remove the two lines of code above the m.play() line to make the video only play and pause once. In this case, when it reaches the end, it is not reset.
Conclusion
The Behaviors panel is a great new feature in Macromedia Flash MX 2004 that provides an easy way to add ActionScript to your movies. The six supplied behaviors allow you to set up custom buttons to control Media Display components without writing any code.
Comments