Macromedia Flash Professional 8 Unleashed

The NetStream Object

The NetStream object allows the control of streaming video through the NetConnection object. The NetStream object has several properties, methods, and an event to assist in controlling the playing of video as well as monitoring its progress.

When instantiating a new NetStream object, you pass it the NetConnection object that the video will go through, like this:

//first create an instance of the object var netCon_nc:NetConnection = new NetConnection(); //now call the connect method netCon_nc.connect(null); //Create the NetStream object var myStream_ns:NetStream = new NetStream(netCon_nc);

Notice that we created a NetConnection object, called the connect() method, and then created an instance of the NetStream object, passing it the NetConnection.

Following are some of the methods and properties for the NetStream object that we will be using throughout this chapter.

The play() Method

The play() method for the NetStream object is used to begin the streaming of a certain Flash Video. You pass the string literal path of the FLV as a parameter in this method like this:

myNetStream_ns.play("myVideo.flv");

When this method is called, if the video is found it will begin to stream. If the video is not found, the onStatus event is invoked.

NOTE

Just calling the NetStream.play() method will not display the video. The video must be attached to a Video object first. However, the audio from the Flash Video will begin to start playing automatically.

The pause() Method

The pause() method can be misleading if used incorrectly. It not only pauses an incoming video, but it can also resume play.

Its generic layout is as follows:

myNetStream_ns.pause(pauseResume);

The only parameter is the pauseResume parameter, which is a Boolean value passed to the method saying whether to pause or resume the video feed:

  • true Pauses the video

  • false Resumes the video from its current position

If the parameter is left out, the pause() method will switch back and forth automatically between resuming and pausing, like a toggle switch.

The close() Method

The close method will stop all streaming media coming into the NetStream instance and will reset the NetStream itself to be used for another video.

Its generic layout looks like this:

myNetStream_ns.close();

The seek() Method

The seek() method allows users to move to a certain point in the stream. It does this by going to the closest keyframe that matches the number of seconds passed into the seek() method. When the stream reaches that point, it resumes playing.

Its generic layout is as follows:

myNetStream_ns.seek(seconds);

The seconds parameter is the number of seconds since the beginning of the video, where playback would preferably begin. You can use this method in several ways:

myStream_ns.seek(20); //moves to the 20 seconds spot myStream_ns.seek(0); //moves to the beginning of the stream myStream_ns.seek(myStream.time 10); //moves to the current position minus 10 seconds

The setBufferTime() Method

The setBufferTime() method is an important one because it controls how many seconds must be in the buffer before the stream can begin playing. Use this method to control playback of the stream, especially on slow connections.

Its generic layout is like this:

myStream_ns.setBufferTime(seconds);

The only parameter is the seconds parameter specifying how many seconds to have in the buffer before playing. The default value is 0.1 (one-tenth of a second).

The onStatus Event

The onStatus event is the only event for the NetStream object, and it is triggered often. Whenever any aspect of a NetStream instance changes, the event is triggered and can pass information about what caused the event.

The generic layout for this event is in a callback like this:

myStream_ns.onStatus = function(infoObj){ //code to run }

The one parameter associated with this event is the infoObj parameter. This parameter is a reference to an object that will store information about the triggering event.

The infoObj parameter has two properties: the code property giving information on what caused the status change, and the level property showing whether it was a simple status change or an error.

Table 26.1 shows the different values for the code property and their meanings.

Table 26.1. Possible Values for the Code Property

Code

Level

Description

NetStream.Play.Start

Status

The stream has begun playing.

NetStream.Play.Stop

Status

The stream has stopped playing.

NetStream.Buffer.Full

Status

The buffer has reached its defined point in seconds, and the stream will begin to play.

NetStream.Buffer.Empty

Status

Data from the stream is not filling the buffer fast enough, and playback will stop until the buffer is full again.

NetStream.Play.StreamNotFound

Error

Flash cannot find the FLV.

All of these properties can be received by being called on the infoObj parameter within the onStatus event like this:

myStream_ns.onStatus = function(infoObj){ trace(infoObj.code); trace(infoObj.level); }

Those were the methods and event for the NetStream object; it also has a few properties.

Properties of the NetStream Object

Following are just a few of the properties of the NetStream object that you might use when working with streaming video:

  • bufferLength The number of seconds currently in the buffer

  • time The position in the stream where the play head is in seconds

  • currentFps The current frames per second the stream is displaying

  • bufferTime The number of seconds that must be in the buffer before it is full

Now you know how to get the video in, but before you can see anything, you have to attach the stream coming in to a video object.

Категории