How to Control Video (FLV) without a Component

ActionScript 2.0 Flash

This earlier post, “How to Load External Video (FLV),” is one of the most popular on this blog, judging by the number of comments.  Just the other day, someone named Enrique thanked me for the sample code, but asked if there was a way to control the FLV file once loaded, since the file is handled without the use of the Media or FLVPlayback Components.  It’s a good question, so let’s take a look at how to accomplish the goal. 

An answer, short and sweet

After typing the code suggested in the other article, type the following ActionScript into a frame of your scripts layer:

// Code from the other article
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
videoPlayer.attachVideo(ns);
ns.play("externalVideo.flv");
// New code ... assumes a button symbol with
// the instance name shown
btnPausePlay.onRelease = function() {
  ns.pause();
}

How’s that for short and sweet?!

How it works

This answer comes from a quick glimpse again at the NetStream class.  We’re already using the NetStream.play() method, in fact.  Here, we’re simply using the NetStream.pause() method, applied to the ns variable, which is our instance of the NetStream class.

The first time this method is called, it pauses play.  After that, the method acts as a toggle — unless the optional parameter is supplied.  If you want two buttons, rather than a toggle, supply that parameter.

btnPause.onRelease = function() {
  ns.pause(true);
}
btnPlay.onRelease = function() {
  ns.pause(false);
}

In the above example, the pause button invokes the NetStream.pause() method, as before, but this time it supplies the optional flag parameter.  If true is supplied, the video is paused, regardless of it’s already paused.  If false is supplied, the video is played from where it was stopped.  If already playing, it keeps on playing.

Experiment with the NetStream.close() and NetStream.seek() methods to stop playing altogether (different from pausing) and to jump to already-loaded portions of a video stream.

Leave a Reply