Accessibility

Table of Contents

Delivering Flash Video: Dynamic Bandwidth Detection with Macromedia Flash Communication Server

Creating the Client-Side Application

Now you can create the application to run on the client using the Macromedia Flash authoring tool:

  1. Create a new folder for your application; I have named mine "bwcheck" but you can name yours differently if you like.
  2. Copy bwcheck.fla into the newly created folder and open the file for editing in Flash.
  3. Highlight Frame 1 of the Actions layer and bring up the Actions panel.

You will see the following code:

1 nc = new NetConnection();
2 nc.onStatus = function(info) {
3 	trace("Level: "+info.level+" Code: "+info.code);
4	if (info.code == "NetConnection.Connect.Success") {
5		trace("--- connected to: " + this.uri);
6		initStream();
7	}
8 };
9 nc.connect("rtmp:/videoplayer");
10
11 initStream = function(){
12	// create a NetStream
13	video_ns = new NetStream(nc);
14	// optional onStatus handler
15	video_ns.onStatus = function(info) {
16		trace("Level: " + info.level + " Code: " + info.code);
17	};
18	// attach the NetStream to a video object called videoObj on stage
19	videoObj.attachVideo(video_ns);
20	video_ns.play("100kbps_Stream", 0); // the parameter 0 will tell the server to play a recorded stream
21 }
22 stop();

This piece of code is what I referred to earlier as the main application. It establishes a NetConnection to the videoplayer application (Lines 1–9). Once connected (Lines 4–7), it calls the initStream method, which in turn creates a new NetStream (Line 13), attaches it to the video object on the Stage (Line 19), and starts playback of the file 100kbps_Stream (Line 20). Note that I omit the .flv extension when referring to the 100kbps_Stream.flv file.

To test the video playback, select Control > Test Movie from the menu or press Control+Enter. The video should play. Should you have problems playing the video, the traces in the Output window may give you some clues to help debugging.

Adding the bwcheck Application to the Mix

Configure this client-side videoplayer using the bandwidth test results from the bwcheck application. Create a new keyframe on Frame 2 and move the existing code into it (see Figure 2).

Actions moved to Frame 2

Figure 2. Actions moved to Frame 2

Now add the following code to Frame 1 of the Actions layer:

1  nc = new NetConnection();
2  nc.onStatus = function(info) {
3  	trace("Level: " + info.level + " Code: " + info.code);
4 	if (info.code == "NetConnection.Connect.Success") {
5 		trace("--- connected to: " + this.uri);
7 	} 
8  };
9  NetConnection.prototype.onBWDone = function(p_bw) {
10 	trace("onBWDone: "+p_bw);
11 }
12 NetConnection.prototype.onBWCheck = function() {
13 	return ++counter; // Serverside, just ignore any return value and return the call count
14 }
15 nc.connect("rtmp:/bwcheck", true);
16 stop();

This code creates a new NetConnection (Line 1) and adds an onStatus handler to it (Lines 2–8). The actual connect call happens on Line 15. Notice that I pass an additional parameter of true in my connect method—this triggers the bandwidth check on the server side. Should you not want to run the speed test upon connection, you should pass false instead. If you pass false, however, the onBWDone method will still be invoked with a value of undefined for p_bw, indicating that no speed test took place. You may simply ignore this value and proceed with your application logic.

Triggering the Speed Test

You may opt to run the speed test at a later time in your application; maybe the download speed is only important to you after a user has proceeded past a certain point in your application. To trigger the speed test, you can invoke the server-side checkBandwidth method like this:

nc.call("checkBandwidth", null);

The corresponding method on the server-side client object that gets triggered by this is the following:

Client.prototype.checkBandwidth = function() {
	application.calculateClientBw(this);
}

In any case, you must establish a NetConnection to the bwcheck application before attempting to run the speed test.

You are now ready to test the movie. Select Control > Test Movie from the menu or press Control+Enter. You should see some traces in the Output window. About two seconds after the successful connection, the onBWCheck method is triggered and the speed test is complete. You can now proceed and use the returned speed results to configure your existing application.