This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Publishing Streams in Detail
The full path will vary depending on the server’s operating system and on how Flash-
Com was installed. See Chapter 4 for more information on resource URIs. In this
example, the stream URI can be broken down into two parts. The path information:
and the actual stream name:
You do not have to create the application’s streams directory or any of its subdirecto-
ries when recording a stream. They are created automatically by the server as needed
when the stream is published.
onStatus( ) handlers
During development, it is a good idea to define a simple onStatus( ) event handler
that simply writes out all the properties of the information object it is passed. An
easy way to do this is to define a method on the
NetStream.prototype object as in
Example 5-1. An onStatus( ) handler defined this way will work for both publishing
and subscribing streams. However, publishing and subscribing streams are sent dif-
ferent sets of messages and are generally designed to behave differently, so it is
unlikely that a generic onStatus( ) handler will apply readily to both types of streams.
Another way to define an onStatus( ) handler is to define it as the method of an indi-
vidual stream:
out_ns = new NetStream(nc);
out_ns.onStatus = function (info) {
if (info.code == "NetStream.Publish.BadName") {
writeln("Invalid stream name. Please enter a different one.");
Yet another approach is to create a NetStream subclass with an onStatus( ) handler
designed just for publishing or just for subscribing. Creating a NetStream subclass is
described later in this chapter and is strongly recommended for applications that
must always know the exact state of a stream at any given time.
If no onStatus( ) handler is defined for a NetStream object (or
NetStream.prototype), the onStatus( ) method of the NetConnection
object to which the stream is attached will be passed any NetStream
information objects with a
level property of “error”. If the NetConnec-
tion.onStatus( ) method is not defined either, then the System.onSta-
tus( ) method, if any, is called.
Attaching and Detaching Audio and Video Sources
When a video or audio source is not needed, it is a good idea to detach it from a
stream instead of leaving it to consume bandwidth. For example, during an online
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Chapter 5: Managing Streams
seminar, participants may need to see only the seminar leader and the person who is
currently speaking. If there are 20 participants in the seminar, there is no point in
sending the audio and video data for 20 people to the server. To temporarily detach
audio or video data from a stream, pass
null to either NetStream.attachAudio( ) or
NetStream.attachVideo( ). You can always call these same methods later to attach the
sources again. For example:
out_ns = new NetStream(nc);
mic = Microphone.get( );
cam = Camera.get( )
out_ns.publish('public/' + userName);
function detachSources ( ) {
function attachSources ( ) {
Streams flow in one direction, and each point within a stream corresponds to a
unique time measured in seconds. While the stream is being published, the
property of a NetStream object can be used to retrieve the number of seconds a
stream has been publishing, provided a data source is attached to the stream. Time
values are floating-point numbers that can provide values down to the millisecond.
For example:
var millisecondTime = out_ns.time * 1000;
If no data is being sent in a stream, the time property will contain the last time data
was sent. When a data source is attached to a publishing stream, the
time value will
restart at the last time value plus the time elapsed during which no data was sent. So
even though the time value may pause when no data is sent, actual stream time
always progresses and is reflected by the
time property when data is again sent on the
To stop publishing a stream without closing it, pass
false to the publish( ) method in
place of the stream URI:
To explicitly close a stream use NetStream.close( ):
out_ns.close( );
Use publish(false) when you plan to republish using the same NetStream object, and
use close( ) when you are done publishing or to reuse the same NetStream object to
subscribe to a stream. When either method is called, any audio, video, and Action-
Script data that has not been sent already will be lost.

Get Programming Flash Communication Server now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.