This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
A Simple Publisher/Subscriber Example
|
153
To create a NetStream object within a network connection, the NetConnection object
must already exist and a connection attempt to FlashCom must have already been
made by calling the connect( ) method. In Example 5-1, a new NetConnection object
is created and then the user must enter both his username and the name of the
remote user he wants to talk to. Then he must click the Connect button. When the
connection is established, the NetConnection.onStatus( ) handler is called and two
NetStream objects are created and initialized to send or receive data. The NetStream
object that is designed to receive data tries to subscribe to a stream immediately, but
the outgoing stream does not attempt to send anything until the user clicks the Send
Stream button.
Publishing a Live Stream
In order to publish a stream, a NetStream object has to be created. In Example 5-1,
the
out_ns object is created within a connection by passing it a NetConnection object.
The keyword
this, which represents the current NetConnection object, is passed in
this example because the NetStream object is created within the NetConnection.
onStatus( ) method:
out_ns = new NetStream(this);
This statement could also have been written using the nc variable defined in the last
line of Example 5-1:
out_ns = new NetStream(nc);
Once the NetStream object out_ns exists, audio and video data sources can be
attached to it:
out_ns.attachAudio(Microphone.get( ));
out_ns.attachVideo(Camera.get( ));
The Microphone and Camera classes are available only within Flash movies (not on
the server) and provide a number of static methods, such as get( ). In the example,
Microphone.get( ) returns the Microphone object last selected in the movie’s Player
Settings dialog box. Passing the Microphone object to the out_ns.attachAudio( )
method makes the sound data captured from the microphone available to the
out_ns
stream. The Camera.get( ) method returns a Camera object and works in the same
way. Attaching the Camera object to the stream using the NetStream.attachVideo( )
method makes compressed video data from the camera available to the stream.
Although it isn’t a best practice to pass the return value from the get( ) methods
directly to the attachAudio( ) and attachVideo( ) methods, we use this approach for
demonstration purposes. Chapter 6 considers other alternatives.
The Camera and Microphone classes provide other methods for managing video and
audio sources and events, and are described in detail in Chapter 6.
Before actually publishing the stream, it is a good idea to set up an onStatus( ) event
handler that will receive notifications when stream events occur. In Example 5-1, a

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.