Controlling video playback

The NetStream class offers four main methods for controlling video playback:

pause(): Pauses playback of a video stream. If the video is already paused, calling this method does nothing.

resume(): Resumes playback of a video stream that is paused. If the video is already playing, calling this method does nothing.

seek(): Seeks the keyframe closest to the specified location (an offset, in seconds, from the beginning of the stream).

togglePause(): Pauses or resumes playback of a stream.

NOTE

 

There is no stop() method. In order to stop a stream you must pause playback and seek to the beginning of the video stream.

NOTE

 

The play() method does not resume playback, it is used for loading video files.

The following example demonstrates how to control a video using several different buttons. To run the following example, create a new document and add four button instances to your workspace (pauseBtn, playBtn, stopBtn, and togglePauseBtn):

var nc:NetConnection = new NetConnection();
nc.connect(null);

var ns:NetStream = new NetStream(nc);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
ns.play("video.flv");
function asyncErrorHandler(event:AsyncErrorEvent):void
{
    // ignore error
}

var vid:Video = new Video();
vid.attachNetStream(ns);
addChild(vid);

pauseBtn.addEventListener(MouseEvent.CLICK, pauseHandler);
playBtn.addEventListener(MouseEvent.CLICK, playHandler);
stopBtn.addEventListener(MouseEvent.CLICK, stopHandler);
togglePauseBtn.addEventListener(MouseEvent.CLICK, togglePauseHandler);

function pauseHandler(event:MouseEvent):void
{
    ns.pause();
}
function playHandler(event:MouseEvent):void
{
    ns.resume();
}
function stopHandler(event:MouseEvent):void
{
    // Pause the stream and move the playhead back to
    // the beginning of the stream.
    ns.pause();
    ns.seek(0);
}
function togglePauseHandler(event:MouseEvent):void
{
    ns.togglePause();
}

Clicking on the pauseBtn button instance while the video is playing causes the video file to pause. If the video is already paused, clicking this button has no effect. Clicking on the playBtn button instance resumes video playback if playback was previously paused, otherwise the button has no effect if the video was already playing.


Flash CS3


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000256.html