Welcome!

Adobe Flex Authors: Liz McMillan, RealWire News Distribution, Maureen O'Gara, Yakov Fain, Keith Swenson

Related Topics: Adobe Flex

Adobe Flex: Article

Building a Simple Live Video Broadcaster and Receiver

Learn how to leverage the power of Flash Communication Server MX to add video and audio communications to your Flash apps

This code initiates the following actions. First it disables the startstop_pb button instance. You will enable it again after a successful connection to the server has been established.

Next comes the onRelease function for the connect_pb button instance. This code emulates a toggle state so that the button can be used to both connect and disconnect based on the current label. At the same time the code outputs status messages into the status_txt field on the Stage to help you visualize what's happening behind the scenes. The following code will disable the broadcast button when you close the connection to your server:

startstop_pb.enabled = false
The actual connection to Flash Communication Server is triggered by this line:
nc.connect(rtmp_txt.text);

As you may have noticed, this code grabs whatever text is currently displayed in the rtmp_txt text input field and passes it as a parameter to the NetConnection object's connect method. This enables you to connect to different servers (or applications) without having to hard code connection strings into the SWF file. If it makes live easier for you, then you can easily prefill the value for this input field inside the Flash authoring environment: Just highlight the component on the Stage, bring up the Properties panel and fill in the text value. Note that you must use the complete rtmp string to your server, including rtmp:/ or rtmp://.

At the bottom of this code block is the onStatus function for your NetConnection object. This function listens for a NetConnection.Connect.Success message and, after receiving one, enables the startstop_pb button instance as you are now ready to create a NetStream function and start broadcasting.

The last code block disables the broadcast button and resets its label in case you get disconnected, either because you have chosen to or maybe because the connection has dropped for unknown reasons. It also cleans up the video object on the Stage by issuing the following:

myvid.attachVideo(null);
myvid.clear();

Step 4: Create a NetStream Object and Publish the Stream
You will create a NetStream object after the startstop_pb button is clicked. The name for the stream will be based on whatever value the streamname_txt text field holds.

Add the following code underneath the existing code in frame 1:


startstop_pb.onRelease = function(){
if (this.label == "Start Broadcast"){
status_txt.text += "Starting broadcast" + newline;
this.label = "Stop Broadcast";
ns = new NetStream(nc);
ns.onStatus = function(info) {
status_txt.text += "NS.onStatus> info.code: " + info.code + newline;
}
mycam = Camera.get();
mycam.setQuality(25600, 0);
mymic = Microphone.get();
mymic.setRate(11);
ns.attachVideo(mycam);
ns.attachAudio(mymic);
myvid.attachVideo(mycam);
ns.publish(streamname_txt.text, "live");
} else {
status_txt.text += "Stopping broadcast" + newline;
this.label = "Start Broadcast";
myvid.attachVideo(null);
myvid.clear();
ns.close();
}
}
This code is fairly self-explanatory:

1.  First (if the current button label is Start Broadcast) it creates a new NetStream object on our existing NetConnection object (ns = new NetStream(nc);).

2.  It then adds an onStatus handler (ns.onStatus = function(info)[...]), which enables you to see trace output inside the status_txt text area.

3.  Next, it grabs a reference to the default camera that's installed mycam = Camera.get(); and calls the setQuality method with a parameter of 25600 bytes and 0 (best possible) image quality at that given bandwidth mycam.setQuality(25600, 0);. Feel free to adjust these settings as you see fit.

4.  Next comes the equivalent action for the default microphone:

5.  mymic = Microphone.get();
mymic.setRate(11);

6.  To be able to send audio and video across the Internet, you must attach both sources to the NetStream object:

7.  ns.attachVideo(mycam); ns.attachAudio(mymic);

8.  Now you need to attach the local camera feed to the video object on the Stage. This will enable you to see the feed that you will be broadcasting. myvid.attachVideo(mycam);

9.  By calling ns.publish(streamname_txt.text, "live") you start the broadcast. You can omit the parameter "live" if you want, because the publish method would actually default to a live stream. However, using this parameter makes matters a little clearer I think.

More Stories By Stefan Richter

Stefan Richter is a Certified Flash Developer and Team Macromedia member who has been involved with Flash Communication Server since its early days. As VP of Application Development and cofounder of POPview, he has developed a variety of Rich Internet Applications using Flash Communication Server, Flash MX 2004 and Coldfusion. You can find more of Stefan's articles at www.flashcomguru.com.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.