Synchronizing a Macromedia Flash Presentation to Streaming Video
Table of Contents
Connecting to the server
Creating a Video object to play the video within the SWF
The first step to playing a streaming video is to give it a place to play within your Macromedia Flash movie. Start by creating a video object. To create a video object within the library, select "New Video" from the Library menu (in the upper right of the panel).
This will create an Embedded Video object in the library. Drag an instance of the Embedded Video object onto the stage. Select it and name the instance vidBoxPlay. Resize the video to match the size of your FLV file. In our case, we were using an unusual size to accommodate all of Jeremy's movement within the smallest size possible.
The following screen shot shows our video object on the stage of our main Flash movie.
Connecting to the server
The following code is used to connect to Macromedia Flash Communication Server MX. We placed this code in the first frame of our movie.
// create connection to server
nc = new NetConnection();
nc.connect(serverName+"/mxoverview");
This code creates a NetConnection called nc that is connected to the mxpresentation application running on the machine named in the serverName variable. We stored the serverName in an XML file so that it could be changed without altering the ActionScript. For more information about how to store and read data from an XML file, see Integrating XML and Flash in a Web Application.
Handle onStatus calls
Macromedia Flash Communication Server MX returns messages to the Macromedia Flash Player in the form of information objects. These objects are sent to a function called onStatus. To use these messages, you must write an onStatus function. We defined the following function for the netConnection object nc.
nc.onStatus = function(returnObj) {
if (returnObj.code == "NetConnection.Connect.Success") {
// the movie is successfully connected to the server
startVideo();
}
else if (returnObj.code == "NetConnection.Connect.Rejected") {
// the server has explicitly rejected the connection
gotoAndStop("server busy");
// reject is followed by a close so you need to clear this out
nc.onStatus=null;
}
else if (returnObj.code == "NetConnection.Connect.Closed") {
// the connection has been closed
gotoAndStop("closed connection");
}
};
ReturnObj is an information object returned from Macromedia Flash Communication Server MX. The object's code property contains the content of the message. For more information on onStatus events, including a complete list of codes, see the Appendix in the Client-Side Communication ActionScript document that comes with Macromedia Flash Communication Server MX.
For the MX Presentation we were interested in three of these codes.
The first, NetConnection.Connect.Success, indicates that the movie is now connected with Macromedia Flash Communication Server MX, in which case we begin the video.
The second, NetConnection.Connect.Rejected, indicates that the server has reached the maximum number of connections (as determined by the main.asc script running on the server) and has rejected our request. When this message is received, the movie goes to a frame called "sever busy" and displays the following message:
"To provide the best user experience possible, we've limited the amount of connections to the server.
Please try again later."
Because each rejected information object is followed by a closed information object, the onStatus function is set to null. This way the closed connection message is ignored.
The only way to generate the rejection message is to do so within the main.asc script. For testing purposes, we simply lowered the maximum bandwidth within our main.asc script before attempting to connect to the server.
The final information code nc.onStatus is looking for is NetConnection.Connect.Closed, which is received whenever the connection is closed (either intentionally or unintentionally). If this status message is received at any time during the presentation, the following message is displayed to the user:
"You have been disconnected from the presentation.
We're sorry you're having trouble.The issue could be:
- The session has timed out due to a period of inactivity
- The Internet connection has been interrupted
To replicate a broken connection, unplug the modem or network cable from your computer while watching the presentation. After the buffer empties, the client generates a NetConnection.Connect.Closed information object and the application should display the message above."