Flash Media Server |
|||
| Client-Side ActionScript Language Reference for Flash Media Server 2 > Client-Side ActionScript Language Reference > NetStream class | |||
|
NOTE |
|
In Flash Player 7 and later, you can use this method to play FLV files without Flash Media Server. For more information, see the NetStream class entry in ActionScript 2.0 Language Reference. |
The NetStream class opens a one-way streaming connection between Flash Player and Flash Media Server through a connection made available by a NetConnection object. A NetStream object is like a channel inside a NetConnection object; this channel can either publish audio and/or video data, using NetStream.publish(), or subscribe to a published stream and receive data, using NetStream.play(). You can publish or play live (real-time) data and previously recorded data.
You can also use NetStream objects to send text messages to all subscribed clients (see NetStream.send()).
The following steps summarize the sequence of actions required for publishing real-time audio and video using Flash Media Server and the Real-Time Messaging Protocol (RTMP):
new NetConnection to create a NetConnection object.NetConnection.connect("rtmp://serverName/appName/appInstanceName") to connect the application instance to the Flash Media Server. new NetStream(connection:String) to create a data stream over the connection.NetStream.attachAudio(audioSource:Microphone) to capture and send audio over the stream, and NetStream.attachVideo(videoSource:Camera) to capture and send video over the stream.NetStream.publish(publishName:String) to give this stream a unique name and send data over the stream to the Flash Media Server, so others can receive it. You can also record the data as you publish it, so that users can play it back later. SWF files that subscribe to this stream will use the name specified here; that is, they will call the same NetConnection.connect() method as the publisher, and then call a NetStream.play(publishName:Object) method. They will also have to call Video.attachVideo() to display the video.
Multiple streams can be open simultaneously over one connection, but each stream either publishes or plays. To publish and play over a single connection, open two streams over the connection, as shown in the following example. This example publishes audio and video data in real time on one stream and plays it back on another stream on the same client, through the same connection.
// These lines begin broadcasting.
var my_nc:NetConnection = new NetConnection(); // Create connection object.
my_nc.connect("rtmp://mySvr.myDomain.com/App"); // Connect to server.
publish_ns:NetStream = new NetStream(my_nc); // Open stream within connection.
publish_ns.attachAudio(Microphone.get()); // Capture audio.
publish_ns.attachVideo(Camera.get()); // Capture video.
publish_ns.publish("todays_news"); // Begin broadcasting.
/* These lines open a stream to play the video portion of the broadcast inside a Video object named my_video. The audio is played through the standard output device--you don't need to issue a separate command to hear the audio. */
var play_ns:NetStream = new NetStream(my_nc);
my_video.attachVideo(play_ns); // Specify where to display video.
play_ns.play("todays_news"); // play() uses the same name as publish() above.
In the previous example, notice that both the publisher and subscriber call an attachVideo() method. The publisher calls NetStream.attachVideo() to connect a video feed to a stream. The subscriber calls Video.attachVideo() to connect a video feed to a Video object on the Stage; the incoming video is displayed inside this object.
Keep in mind also that although the publisher calls an attachAudio() method, the subscriber does not. This is because audio sent through a stream is played through the subscriber's standard audio output device by default; the subscriber doesn't have to attach the incoming audio to an object on the Stage. However, you can use MovieClip.attachAudio() to route audio from a NetStream object to a movie clip. If you do this, you can then create a Sound object to control the volume of the sound. For more information, see MovieClip.attachAudio().
|
Method |
Description |
|---|---|
|
Publisher method; specifies whether audio should be sent over the stream. |
|
|
Publisher method; starts transmitting video or a snapshot from the specified source. |
|
|
Stops publishing or playing all data on the stream and makes the stream available for another use. |
|
|
Subscriber method; pauses or resumes playback of a stream. |
|
|
Subscriber method; feeds streaming audio, video, and text messages being published on the Flash Media Server, or a recorded file stored on the server, to the client. |
|
|
Publisher method; sends streaming audio, video, and text messages from the client to the Flash Media Server, optionally recording the stream during transmission. |
|
|
Subscriber method; specifies whether incoming audio plays on the stream. |
|
|
Subscriber method; specifies whether incoming video will play on the stream, or specifies the frame rate of the video. |
|
|
Subscriber method; seeks to a position in the recorded stream currently playing. |
|
|
Publisher method; broadcasts a message to all subscribing clients. |
|
|
Method; for a publishing stream, this number indicates how long the outgoing buffer can grow before Flash starts dropping frames. For a subscribing stream, this number indicates how long to buffer incoming data before starting to display the stream. |
|
Property (read-only) |
Description |
|---|---|
|
The number of seconds of data currently in the buffer. |
|
|
The number of seconds assigned to the buffer by NetStream.setBufferTime(). |
|
|
The number of frames per second being sent or received on the publishing or subscribing stream. |
|
|
The number of seconds of data in a subscribing stream's buffer in live mode. |
|
|
The number of seconds a stream has been playing or publishing. |
|
Method |
Description |
|---|---|
|
Invoked when an embedded cue point is reached while playing an FLV file. |
|
|
Invoked when the Flash Player receives descriptive information embedded in the FLV file being played. |
|
|
Invoked when a NetStream object has completely played a stream. |
|
|
Invoked every time a status change or error is posted for the NetStream object. |
new NetStream(connection)
connection A NetConnection object.
A NetStream object.
Constructor; creates a stream that can be used for publishing (sending) or playing (receiving) data through the specified NetConnection object.
You can't publish and play data over the same stream at the same time. For example, if you are publishing on a stream and then call NetStream.play(), an implicit NetStream.close() method is called; the publishing stream then becomes a subscribing stream.
However, you can create multiple streams that run simultaneously over the same connection: one stream publishes and another stream plays.
The following example shows how a publishing client and a subscribing client can connect to the Flash Media Server and then open an application stream for sending (publishing) or receiving (playing) data over this connection.
// Publishing client contains this code.
my_nc:NetConnection = new NetConnection(); // create NetConnection object
my_nc.connect("rtmp://myRTMPServer.myDomain.com/app"); // connect to server
my_ns:NetStream = new NetStream(my_nc); // open app stream within my_nc
my_ns.publish("myWeddingVideo"); // publish data over this stream
// Subscribing client contains this code.
/* Note that the connection and stream names are the same as those used by the publishing client. This is neither required nor prohibited,because the scripts are running on different machines.However,the parameters used with connect() and play() below must be the same as those used with connect() and publish() above. */
my_nc:NetConnection = new NetConnection();
my_nc.connect("rtmp://myRTMPServer.myDomain.com/app");
my_ns:NetStream = new NetStream(my_nc);
my_ns.play("myWeddingVideo");
For more examples, see the NetStream class entry and Video.attachVideo().
NetConnection class, NetStream.attachAudio(), NetStream.attachVideo(), NetStream.play(), NetStream.publish(), Video.attachVideo()
RSS feed | Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/fms/2/docs/00000575.html
Comments
_flashman_ said on Jul 24, 2007 at 8:47 AM :