Starting, pausing, and stopping a video are very common controls that are needed when programming video user interfaces. Note that these controls can be accessed through the FLVPlayback custom UI components included in Flash CS4 Professional. The custom UI components are simple to use, however there will be many times when you'll need to access the FLVPlayback commands directly via programming as demonstrated in the next part of this article.
This section covers the following topics:
You'll probably find that most custom functionality with video revolves around events generated in the video's life cycle. But at the core of it all, you will also use a lot of simple commands like those shown in the list below. Notice how each command starts with the instance name of the FLVPlayback component—in this case, we've named our instance of the FLVPlayback component display.
To apply a play command, use the following code:
display.play();
The play command now supports three parameters: source, totalTime, and isLive. You can initialize these values in the constructor similar to the following code:
display.play("videos/myVideo.flv", 32, false);
To apply a pause command, use the following code:
display.pause();
To apply a stop command, use the following code:
display.stop();
The ability to loop a video can be very useful. The following steps show how to listen for the video's complete event and then restart playback from the beginning.
To create a looping video:
import fl.video.*;
// Video component instance name
var flvControl:FLVPlayback = display;
var flvSource:String = "Call_to_Action.flv";
// Loop the video when it completes
function completeHandler(event:VideoEvent):void
{
flvControl.seek(0);
flvControl.play()
}
flvControl.addEventListener(VideoEvent.COMPLETE, completeHandler);
// Set video
flvControl.source = flvSource;
You can use the FLVPlayback's ActionScript API to create your own custom buttons and controls. The following steps show the code that toggles playback on and off by using a custom button.
To toggle play and pause:
// Add toggle code
function toggleHandler(event:MouseEvent):void
{
if( flvControl.playing ){
flvControl.pause();
}else{
flvControl.play();
}
}
toggle_btn.addEventListener(MouseEvent.CLICK, toggleHandler);
Once you have a looping video in place, it's a good idea to provide a way to stop the video from playing. The following code shows you how to stop and reset a video to stop it from looping.
To stop and reset:
// Add reset code
function resetHandler(event:MouseEvent):void
{
flvControl.autoRewind = true;
flvControl.stop();
}
reset_btn.addEventListener(MouseEvent.CLICK, resetHandler);
To see a working example, see flvplayback_programming3.fla in the sample files.