This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
A Simple Publisher/Subscriber Example
|
155
Even if the publishing stream defines a showMessage( ) method, it will not receive
any remote method calls it sends on the stream. Only subscribed streams receive
remote method calls.
In summary, the steps to publish a live stream are:
1. Create a NetConnection object:
nc = new NetConnection( );
2. Use NetConnection.connect( ) to attempt to connect to an application instance.
The connect( ) method must be called before attempting to create a NetStream
object:
nc.connect("rtmp:/courseChat/algebra101", userName);
3. Create a new NetStream object on a NetConnection object by passing the latter
as a parameter to the NetStream constructor:
out_ns = new NetStream(nc);
4. Attach any audio or video sources to the stream using attachAudio( ) and attach-
Video( ) methods of the NetStream class:
out_ns.attachAudio(Microphone.get( ));
out_ns.attachVideo(Camera.get( ));
5. Make sure the NetStream object has an onStatus( ) handler, either by defining
one on
NetStream.prototype or by creating a method for the individual Net-
Stream object. This step is not required but is recommended.
6. Publish the stream using NetStream.publish( ):
out_ns.publish("public/" + userName);
Subscribing to a Live Stream
Subscribing to a stream is a little different than publishing one. To publish audio and
video in a stream, we attached a Camera and a Microphone object to the stream.
However, to play a stream and display it in the Flash client, we have to attach the
stream to a Video object. We also have the option of attaching the stream to a movie
clip to control the stream’s audio. We always start by creating a NetStream object
within a NetConnection and then attach objects that display video or control audio.
Methods can also be added to the stream to receive remote method calls. Here, we
create a new NetStream, attach it to a Video object, and play the stream. Text mes-
sages received by the stream will be displayed using the showMessage( ) method of
the NetStream object:
in_ns = new NetStream(nc);
remote_video.attachVideo(in_ns);
in_ns.showMessage = function (msg) {
writeln(msg);
};
in_ns.play("public/" + remoteUserName);
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
156
|
Chapter 5: Managing Streams
In Example 5-1, the in_ns stream is attached to an embedded Video object named
remote_video. Embedded Video objects are similar to movie clips. They can be
placed on the Stage to display video and have some of the same properties as movie
clips. For example, you can manipulate the height and width of an embedded Video
object by setting its
_height and _width properties. To place a Video object on the
Stage, use the Library panel’s pop-up Options menu and choose New Video. Once
an embedded Video object is available in the Library, it can be dragged to the Stage
and resized, or it can be placed within a movie clip. Once on stage, a Video object
must be given an instance name using the Properties panel in order to refer to it via
ActionScript.
By default, audio arriving within a stream is automatically forwarded to a sound
device on the local computer so that it can be heard. It is not necessary to attach a
stream to anything in order to hear audio. However, to control audio volume, the
stream can be attached to a movie clip and then a Sound object can be created on the
movie clip to control the volume of the audio. For example, to set the volume to 50,
an existing movie clip instance named
stream_mc could be utilized this way:
stream_mc.attachAudio(in_ns);
soundControl = new Sound(stream_mc);
soundControl.setVolume(50);
Although the Sound object did not work properly in the Flash Player 6
initially distributed with the Flash MX development environment,
later versions properly control stream volume. The Player used in the
authoring environment is not automatically upgraded when the
browser Player plugin is upgraded. To upgrade the Player used in the
Flash development environment, visit:
http://www.macromedia.com/support/flash/downloads.html
In summary, NetStream objects have attachVideo( ) and attachAudio( ) methods that
are used to attach video and audio sources when publishing a stream. Video objects
have an attachVideo( ) method that is used to attach video arriving within a stream to
an embedded Video object. Similarly, the MovieClip class has an attachAudio( )
method that attaches incoming audio to a clip.
If a publishing stream calls a remote method, that method must exist on the sub-
scribing stream or nothing will happen. In Example 5-1, the NetStream.send( )
method is called and passed the remote method named showMessage( ) and a text
string to pass into it. Defining a showMessage( ) method on the subscribing stream,
as shown earlier, is all that is required to respond to the remote method call.
Once the
in_ns stream is ready to receive audio, video, and remote method calls, you
can subscribe it to a stream on the server using NetStream.play( ):
in_ns.play("public/" + remoteUserName);

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.