Flash Media Server Developer Documentation

NetStream class

The NetStream class is a one-way stream 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 data, video data, or both, by using NetStream.publish(); or it can subscribe to a published stream and receive data, by using NetStream.play(). You can publish or play live (real-time) data and previously recorded data.

Note: For information about what types of media Flash Media Server supports, see Adobe Flash Media Server Technical Overview.

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 to publish real-time audio and video by using Flash Media Server and the Real-Time Messaging Protocol (RTMP):

  1. Use new NetConnection() to create a NetConnection object.
  2. Use NetConnection.connect("rtmp://serverName/appName/appInstanceName") to connect the application instance to the Flash Media Server.
  3. Use new NetStream(connection:String) to create a data stream over the connection.
  4. Use NetStream.attachAudio(audioSource:Microphone) to capture and send audio over the stream, and use NetStream.attachVideo(videoSource:Camera) to capture and send video over the stream.
  5. Use NetStream.publish(publishName:String) to give this stream a unique name and send data over the stream to the Flash Media Server, so that 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 use the name specified here; that is, they call the same NetConnection.connect method as the publisher, and then call a NetStream.play(publishName:Object) method. They also have to call Video.attachVideo() to display the video.

Multiple streams

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 the 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().

Data keyframes

Note: This feature is available in Flash Media Server 3; Flash Player 9 Update 3.

After creating the NetConnection and NetStream objects, you can use the NetStream.send() method to add metadata to live audio or video as you stream it to the server. Metadata can be information such as the height or width of a video, its duration, the name of its creator, and so on.

To define the metadata, use the special handler name @setDataFrame as the first parameter to NetStream.send().

The client that receives the live stream also needs to define an onMetaData() event handler to retrieve the metadata from the stream (see the description of NetStream.send() for details).

Availability

Flash Communication Server 1.0; Flash Player 6.

Method summary

Method

Description

NetStream.attachAudio()

Publisher method; specifies whether audio should be sent over the stream.

NetStream.attachVideo()

Publisher method; starts capturing video or a snapshot from the specified source.

NetStream.close()

Stops publishing or playing all data on the stream and makes the stream available for another use.

NetStream.pause()

Subscriber method; pauses or resumes playback of a stream.

NetStream.play()

Subscriber method; plays streaming audio, video, and text messages being published to Flash Media Server, or plays a recorded stream stored on the server.

NetStream.publish()

Publisher method; sends streaming audio, video, and text messages from the client to Flash Media Server, optionally recording the stream during transmission.

NetStream.receiveAudio()

Subscriber method; specifies whether incoming audio plays on the stream.

NetStream.receiveVideo()

Subscriber method; specifies whether incoming video play on the stream, or specifies the frame rate of the video.

NetStream.seek()

Subscriber method; seeks to a position in the recorded stream currently playing.

NetStream.send()

Publisher method; broadcasts a message to all subscribing clients.

NetStream.setBufferTime()

Behavior depends on whether this method is called on a publishing or a subscribing stream. For a publishing stream, this number indicates how long the outgoing buffer can grow before Flash Player starts dropping frames. For a subscribing stream, this number indicates how long to buffer incoming data before starting to display the stream.

Property summary

Property (read-only)

Description

NetStream.bufferLength

The number of seconds of data currently in the buffer.

NetStream.bufferTime

The number of seconds assigned to the buffer by NetStream.setBufferTime().

NetStream.currentFps

The number of frames per second being sent or received on the publishing or subscribing stream.

NetStream.liveDelay

The number of seconds of data in a subscribing stream's buffer in live mode.

NetStream.time

For a subscriber stream, the number of seconds the stream has been playing; for a publishing stream, the number of seconds the stream has been publishing.

Event handler summary

Event

Description

NetStream.onCuePoint()

Invoked when an embedded cue point is reached while a video file is playing.

NetStream.onMetaData()

Invoked when Flash Player receives descriptive information embedded in the video file that is playing.

NetStream.onPlayStatus()

Invoked when a NetStream object has completely played a stream.

NetStream.onStatus()

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

NetStream.onTextData()

Invoked when Flash Player receives text data embedded in a media file that is playing.