Welcome!

Adobe Flex Authors: Maureen O'Gara, Liz McMillan, RealWire News Distribution, 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

For more information on setQuality, setRate and all the other Flash Communication Server-related objects and methods you should consult the excellent Flash Communication Server Documentation and Live Docs.

You are now ready for your first test run. Press Control+Enter or select Control > Test Movie to see what happens. Enter a valid rtmp address into the first text box (for example, rtmp:/livecast for local servers or rtmp://yourserver.com/livecast for remote servers), as well as a stream name into the text box below below. Click Connect. Your screen should look somewhat similar to Figure 3 (the cheeky grin is optional).

Did it work? Great. Pat yourself on the back-now let's build the live video receiver.

Building the Live Video Receiver
The receiver is simply an adaptation of the broadcasting application. Make sure you save the broadcaster.fla file and then save a copy under the new name of receiver.fla.

Here's what your receiver needs to do:

  1. Connect to Flash Communication Server.
  2. Create a receiving NetStream object.
  3. Display the stream. Sounds like you merely need to strip back some functionality rather than add anything new.
Start by removing the startstop_pb button, because you no longer need it. You can also remove the accompanying function from your code. You should delete startstop_pb.enabled = false on line 1 as well as the startstop_pb.onRelease function at the bottom of your code.

Also, the connect_pb.onRelease function can be stripped of the following line:

startstop_pb.enabled = false;

In fact, you can remove any references to startstop_pb.


connect_pb.onRelease = function(){
if(this.label == "Connect"){
status_txt.text += "Connecting..." + newline;
this.label = "Disconnect";
nc.connect(rtmp_txt.text);
} else {
status_txt.text += "Disconnecting." + newline;
this.label = "Connect";
nc.close();
}
}
The NetConnection object's onStatus handler also needs some minor modifications. After a "NetConnection.Connect.Success" message is received, you need to create a NetStream object, add an onStatus handler to it (in order to see some trace output on the Stage) and attach the stream to your video object (myvid.attachVideo(ns)) in order to display the feed. There is no need to anything similar to the microphone or sound object, because the sound that is broadcast is contained within the same NetStream object. Therefore attachVideo in this case actually means attaching video and sound.

To finish, add the ns.play command at the end. Here's the completed code:


nc = new NetConnection();
nc.onStatus = function(info) {
status_txt.text += "NC.onStatus> info.code: " + info.code + newline;
if (info.code == "NetConnection.Connect.Success") {
status_txt.text = "Connected to " + this.uri + newline;
ns = new NetStream(nc);
ns.onStatus = function(info) {
status_txt.text += "NS.onStatus> info.code: " + info.code + newline;
}
myvid.attachVideo(ns);
ns.play(streamname_txt.text, -1)
}
}
As you can see instead of attaching a camera and microphone to the NetStream object we instead call the play method and pass it the current value of streamname_txt as its first parameter. Of course this parameter (the name of the stream which should be played back) needs to match the name of the stream you will be broadcasting through the broadcaster application.

Time for another pat on the shoulder - you are now ready to go on the air.

Lights, Camera, Action
Publish both movies, then open them side by side. You can either use two browser windows or two standalone Flash Player applications - the choice is yours.

Press the Connect button in both applications, followed by the Start Broadcast button in your broadcaster application. If you followed my directions, then your screen should look similar to Figure 4 (minus that funny-looking guy with glasses).

If you are having problems connecting to your server or the application isn't working as expected, then don't worry - you can download the completed source files from page 1 of this tutorial, and you can modify those to suit your needs.

First, however, make sure that you are using the correct rtmp string and that the server you are trying to connect to is actually running. Don't laugh, it happens all the time.

Where to Go from Here
The example in this tutorial should give you a good basis for additional features. You may want to add a slider component to change your camera's broadcast settings on the fly. Or how about adding a NetStream.send command to send text information across the live stream? Or even simpler, try adding a microphone mute button, which enables you to start and stop audio transmissions independently from the video stream.

Please visit my forum at www.flashcomguru.com/forum/default.asp where you will find a thread accompanying this article.

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.