Flash Media Server Developer Documentation

NetStream.publish()

public publish(name:String [, howToPublish:String] ) : Void

Sends streaming audio, video, and text messages from the client to Flash Media Server, optionally recording the stream during transmission. This method is available only to the publisher of the specified stream.

Availability

Flash Communication Server 1.0; Flash Player 6.

Parameters

name A string value that identifies the stream. If you pass false, the publish operation stops. Subscribers to this stream must pass this same name when they call NetStream.play(). You don't need to include a file extension for the stream name.

Important: Don't end a name with a trailing slash. For example, don't use the stream name "bolero/".

howToPublish An optional string that specifies how to publish the stream. Valid values are "record", "append", and "live". The default value is "live".

  • If you pass "record" for howToPublish, the stream is published and the data is recorded to a new file named name.flv. The file is stored on the server in a subdirectory within the directory that contains the server application. If the file already exists, it is overwritten.

Note: All recorded streams are saved as FLV files.

  • If you pass "append" for howToPublish, the stream is published and the data is appended to a file named name.flv, stored on the server in a subdirectory within the directory that contains the server application. If no file named name.flv is found, it is created.
  • If you omit this parameter or pass "live", Flash Player publishes live data without recording it. If name.flv exists, it is deleted.

Note: If name.flv is read-only, live data is published and name.flv is not deleted.

Details

Don't use this method to play a stream that has already been published and recorded. For example, suppose that you have recorded a stream named "allAboutMe". To enable someone to play it back, just open a stream for the subscriber and call NetStream.play():

var publish_ns:NetStream = new NetStream(my_nc);
var subscribe_ns:NetStream = new NetStream(my_nc);
subscribe_ns.play("allAboutMe");

When you record a stream, the server creates an FLV file and stores it in a subdirectory of the application's directory on the server. The server creates these directories automatically; you don't have to create one for each instance name.

/* Connect to an instance of an app stored in
a directory named "lectureSeries" in your applications directory.
A file named "lecture.flv" is stored in a subdirectory named
"...\yourAppsFolder\lectureSeries\streams\Monday". */

var my_nc:NetConnection = new NetConnection();
my_nc.connect("rtmp://server.domain.com/lectureSeries/Monday");
var my_ns:NetStream = new NetStream(my_nc);
my_ns.publish("lecture", "record");

/* Connect to a different instance of the same app 
but issue an identical publish command.
A file named "lecture.flv" is stored in a subdirectory named
"...\yourAppsFolder\lectureSeries\streams\Tuesday". */
var my_nc:NetConnection = new NetConnection();
my_nc.connect("rtmp://server.domain.com/lectureSeries/Tuesday");
var my_ns:NetStream = new NetStream(my_nc);
my_ns.publish("lecture", "record");

If you don't pass a value for instanceName, name.flv is stored in a subdirectory named "...\yourAppsFolder\appName\streams\_definst_" (for "default instance"). For more information on using instance names, see NetConnection.connect(). For information on playing back files, see NetStream.play().

This method can invoke NetStream.onStatus() with a number of different information objects. For example, if someone is already publishing on a stream with the specified name, NetStream.onStatus() is called with a code property of NetStream.Publish.BadName. For more information, see NetStream.onStatus().

Example

The following example shows how to publish and record a video and then play it back. Create a subdirectory of the applications directory called publishTest. After you record a stream (you'll need a camera), you can find the stream in the applications/publishTest/streams/_definst_ directory.

var my_nc:NetConnection = new NetConnection();
my_nc.connect("rtmp:/publishTest");

var publish_ns:NetStream = new NetStream(my_nc);
publish_ns.publish("allAboutMe", "record");
publish_ns.attachVideo(Camera.get());

var subscribe_ns:NetStream = new NetStream(my_nc);
subscribe_ns.play("allAboutMe");

// my_video is a Video object on the Stage.
my_video.attachVideo(subscribe_ns);

See also

NetConnection.connect(), NetStream.play(), Video.attachVideo()