View comments | RSS feed

NetStream class

Availability

Flash Player 7.

Note: This class is also supported in Flash Player 6 when used with Flash Communication Server. For more information, see the Flash Communication Server documentation.

Description

The NetStream class provides methods and properties for playing Flash Video (FLV) files from the local file system or an HTTP address. You use a NetStream object to stream video through a NetConnection object. Playing external FLV files provides several advantages over embedding video in a Flash document, such as better performance and memory management, and independent video and Flash frame rates. This class provides a number of methods and properties you can use to track the progress of the file as it loads and plays, and to give the user control over playback (stopping, pausing, and so on).

For more information on video playback, see "Playing back external FLV files dynamically" in Using ActionScript in Flash.

Method summary for the NetStream class

The following methods and properties of the NetConnection and NetStream classes are used to control FLV playback.

Method Purpose
NetStream.close()

Closes the stream but does not clear the video object.

NetStream.pause()

Pauses or resumes playback of a stream.

NetStream.play()

Begins playback of an external video (FLV) file.

NetStream.seek()

Seeks a specific position in the FLV file.

NetStream.setBufferTime()

Specifies how long to buffer data before starting to display the stream.

Property summary for the NetStream class

Property Description
NetStream.bufferLength

The number of seconds of data currently in the buffer.

NetStream.bufferTime

Read-only; the number of seconds assigned to the buffer by NetStream.setBufferTime().

NetStream.bytesLoaded

Read-only; the number of bytes of data that have been loaded into the player.

NetStream.bytesTotal

Read-only; the total size in bytes of the file being loaded into the player.

NetStream.currentFps

The number of frames per second being displayed.

NetStream.time

Read-only; the position of the playhead, in seconds.

Event handler summary for the NetStream class

Event handler Description
NetStream.onStatus

Invoked every time a status change or error is posted for the NetStream object.

Constructor for the NetStream class

Availability

Flash Player 7.

Note: This class is also supported in Flash Player 6 when used with Flash Communication Server. For more information, see the Flash Communication Server documentation.

Usage

new NetStream(my_nc:NetConnection) : NetStream

Parameters

my_nc A NetConnection object.

Returns

A reference to a NetStream object.

Description

Constructor; creates a stream that can be used for playing FLV files through the specified NetConnection object.

Example

The following code first constructs a new NetConnection object, connection_nc, and uses it to construct a new NetStream object called stream_ns. Select New Video from the Library options menu to create a video object instance, and give it an instance name my_video.Then add the following ActionScript to your FLA or AS file:

var connection_nc:NetConnection = new NetConnection();
connection_nc.connect(null);
var stream_ns:NetStream = new NetStream(connection_nc);
my_video.attachVideo(stream_ns);
stream_ns.play("video1.flv");

See also

NetConnection class, NetStream class, Video.attachVideo()


Comments


style11 said on Jan 6, 2005 at 2:03 PM :
When using NetStream inside of a function or class, always make sure the NetConnection you use in not a function variable, ie, do NOT declare it like this:

var netConn:NetConnection = new NetConnection();
Flash-Ripper.com said on Jan 24, 2005 at 9:45 AM :
How developer can get a total time (duration) of video being loaded? There's no special NetStream object property or method to accomplish this task. The only way i see is to get duration parameter via netStream.onMetadata event handler together with other meta information:

nc = new NetConnection();
nc.connect(null);
ns = new NetStream(nc);
ns.onMetaData = function(obj) {
trace("Video Duration is: "+obj.duration);
trace("Video Rate is:"+obj.videodatarate);
trace("Audio Rate is:"+obj.audiodatarate);
};
mcVideo.attachVideo(ns);
ns.play("testvideo.flv");

But, unhappily, this event handler still not documented in Flash Help and not declared in NetStream class so developer must be an prophet or google maniac to find this solution.

Is there any arguments against netStream.onMetadata event handler or this is just a miserable mistake? Can we hope onMetadata will be well implemented in next update/version of Flash and Flash Help? Please tell.
John Hall said on Feb 4, 2005 at 9:26 AM :
So if you're trying to use this inside a function, don't type the objects. It's more like this:

function playVid(fName) {
netConn = new NetConnection();
netConn.connect(null);
netStream = new NetStream(netConn);
my_video.attachVideo(netStream);
netStream.setBufferTime(5);
netStream.play(fName);
}
dtaggart3 said on Apr 14, 2005 at 1:32 PM :
ns.onMetaData = function(obj) {
trace("Video Duration is: "+obj.duration);
trace("Video Rate is:"+obj.videodatarate);
trace("Audio Rate is:"+obj.audiodatarate);
};

this code returns the error msg : There is no property with the name 'onMetaData'.
dafreq said on Apr 20, 2005 at 3:06 AM :
to make onMetaData work you need to add the line-
function onMetaData(info:Object):Void;

into
Flash MX 2004\en\First Run\Classes\NetStream.as intrinsic class NetStream
No screen name said on Apr 27, 2005 at 3:35 AM :
and to make onMetaData work, you need to have FLV 1.1 files too (FLV with meta)...
and Flash's converter doesn't seem to output FLV 1.1 files...
so I use Riva Encoder for this : http://www.rivavx.com/
No screen name said on May 6, 2005 at 9:20 PM :
I want to time line bar on Client use NetStream, RTMP of FCS, help me !
de:jay said on May 10, 2005 at 9:57 AM :
This says that the NetStream.close() doesn't clear. So, how do I clear a video object?
Helper Bee said on May 10, 2005 at 10:08 PM :
de:jay - I believe you want Video.clear(). http://livedocs.macromedia.com/flash/mx2004/main_7_2/00001874.html. That should be a See also link in the Netstream.close() entry. Thanks for pointing this out.
mbd said on Jun 1, 2005 at 2:59 PM :
style11: You saved me, potentially, hours of frustration!

I took the code from the docs and dropped it inside a function, so I could click a button and see the video. It didn't work. I looked all over the docs trying to figure out what I had done wrong, but all the examples show this same example. Granted, the example code does not sit inside a function, but nowhere do the docs mention NOT to do this. I realize you can't mention all the don'ts, but this would be a particularly common mistake, I presume.

Why is this not documented?
ajk_99 said on Jun 23, 2005 at 5:02 AM :
Thanks to the top comment I have this working in a function, but still can't get it to work in a class. Am I missing something? When I call the playMovie method nothing happens (though myNc and myNs do get instantiated).

class Cinema
{
var myNc:NetConnection;
var myNs:NetStream;
var cinemaSymbol:MovieClip; //which contains an embedded video symbol named vid

//constructor
function Cinema(mc:MovieClip)
{
cinemaSymbol = mc;
//Other constructor stuff ...
}

//Other cinema methods

function playMovie(videoTitle:String):Void
{
myNc = new NetConnection();
myNc.connect(null);
myNs = new NetStream(myNc);
cinemaSymbol.vid.attachVideo(myNs);
myNs.play(videoTitle);
}
}
Mo Diggz said on Jul 30, 2005 at 11:26 PM :
ajk_99,

I experienced the same issue style11 and Flash-Ripper.com mentioned. I too was stopped in my tracks with the same situation your describing and I solved this problem by creating a Symbol wihin the Library and then setting it's Linkage properties to "Export for ActionScript" and its "Identifier" and "AS 2.0 Class" was set to the name of my class. In your case your Identifier would be "Cinema" and your AS 2.0 Class would be "Cinema" as well. I hope this works for you as too as I had spent countless hours banging my head against the wall trying to figure out why my video woudn't play from wihtin a class definition.
No screen name said on Aug 16, 2005 at 4:22 PM :
Just encountered a problem with dynamically attaching video objects embeded in movieclips. Apparently when you attach a movieclip containing a video object, the video object isn't completely available. You need to delay for about 50 ms before you can use it with NetStream. This bug drove me crazy for a good few hours. Hopefully you won't run into the same issue.
jnicol said on Aug 18, 2005 at 2:11 AM :
A note about meta data. In MX 2004 I believe you can avoid the need to modify the ns class by accessing meta data in this manner:

nets["onMetaData"] = function...
No screen name said on Aug 18, 2005 at 10:24 AM :
I must be missing something. I am importing a FLV file called "BikeVideo.flv" into my embedde video component "Imp_Movie_vc" on the first frame of the movie I have this actionscript:

Net_Con = new NetConnection();
Net_Con.connect(null);
Net_Stream = new NetStream(NetCon);
Imp_Movie_vc.attachVideo(Net_Stream);
Net_Stream.setBufferTime(5);
Net_Stream.play("BikeVideo.flv");

When I publish nothing happens. Do I need to post to an actual server to preview the movie.
de:jay said on Aug 18, 2005 at 10:56 AM :
This line "Net_Stream = new NetStream(NetCon);"
seems to need an "_" (underscore):

Net_Stream = new NetStream(Net_Con);

j
kfur said on Sep 26, 2005 at 12:18 PM :
I too have been trying to get a streaming videos total length for use in my
streaming video player.
I had no problem getting the duration from the onMetadata info object
when I used progressive flv's but once they were on the flash com server
this functionality stopped working. I found this article from
METASHPHERE that talks about using server side actionscript to solve
this issue:
http://www.flvplayer.com/questions.php#delivery_methods
But there still seems to be the problem of the onMetadata HACK that is
adding the function onMetaData(info:Object):Void; to the first run
folder>netstream.as file
Xmpcray said on Oct 29, 2005 at 7:26 AM :
style11: Thanks for the great tip...it was the local variable for netconnection which was causing the problem!
markyd said on Oct 29, 2005 at 9:59 PM :
jnicol,

this also seems to work:

NetStream.prototype.onMetaData = function(obj){ ...etc
jdehaan said on Nov 10, 2005 at 5:50 PM :
NetStream.onMetaData is documented in Flash 8. Please see this page:

http://livedocs.macromedia.com/flash/8/main/00002562.html

 

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

Current page: http://livedocs.adobe.com/flash/mx2004/main_7_2/00001589.html