Accessibility

Table of Contents

Beginner's guide to using ActionScript 3.0 with Flash Media Server 3.5

Creating a streaming video application

So far, in this series, you have essentially been operating on autopilot and letting FMS 3.5 automatically feed a video stream from the vod folder into the FLVPlayback component or a video object on the Stage. There will come a time when the vod folder is more of a hindrance than an asset, however. Also, the vod folder is a feature of Flash Media Streaming Server. If you are using Flash Media Interactive Server, you will need to know how to create an application that plays from this server.

Actually, it is not terribly mysterious to use, but if you are new to FMS 3.5 it can be a bit confusing. To start with, your publishing point is not a vod folder; it is your applications folder. This folder is placed in the applications folder of FMS 3.5 but it is named by you.

The next big difference is the use of an instance. This has nothing to do with ActionScript. An instance is a specific folder named _definst_ that you must create and into which you place the video or audio files.

In addition, there is usually an .as file in the application folder that contains some relatively simple server-side code. Take a look at what I am talking about:

  1. Inside the download folder is a folder called Exercise. Open it. Inside is a folder named BabyVultures. This is the application you will be using.
  2. Open this folder and you will see a folder named streams and a file named main.asc (see Figure 3). This file must be named main.asc; it contains the server-side code that makes stuff work.

    Note: I would like to thank my colleague Lisa Larson-Kelley for the use of this particular main.asc file.

    Basic folder structure for an FMS application

    Figure 3. Basic folder structure for an FMS application

    An .asc file is created by selecting ActionScript Communication File from the Flash Start Panel in Flash CS4 Professional (see Figure 4) or by selecting File > New and selecting ActionScript Communication File in the New Document dialog box.

    Creating an ActionScript communication file

    Figure 4. Creating an ActionScript communication file in the Flash CS4 start screen

  3. Open the main.asc file and you will see how simple this server-side code really is:

    application.onConnect = function(p_client)
    {
      this.acceptConnection(p_client);
    } 

    All that this code says is that when the movie start playing, the FMS 3.5 server is ready to accept the connection. This is an extremely simple example of a main.asc file. As you start moving into the realm of shared objects and even the use of custom components, this code can become very complex and run hundreds of lines.

  4. Close the open Flash document and open the streams folder. Inside the streams folder is the _definst_ folder. This is where the media gets placed. In this case, if you open _definst_, you will find a copy of the Vultures.mp4 file.

Now that you know how these things are structured, it's time to put your new knowledge to good use and stream the video:

  1. Open the applications folder, C:\Program Files\Adobe\Flash Media Server 3.5\applications, and copy the BabyVultures folder from the Exercise folder to the applications folder. You have just created an application named BabyVultures whose path is C:\Program Files\Adobe\Flash Media Server 3.5\applications\BabyVultures. Now all you need to do is to "wire up" the application.
  2. Double-click the H264_StreamApp.fla file in the Exercise folder to open it in Flash CS4 Professional.
  3. Click once in the first frame of the actions layer and open the Actions panel.
  4. When the Actions panel opens, click once in the Script pane and enter the following code:

    var nc:NetConnection = new NetConnection();
    nc.connect("rtmp://localhost/BabyVultures");
     
    var ns:NetStream = new NetStream(nc);

    The big difference from the previous example is in the NetConnection string in line 2. The publishing point has changed from the vod folder, which you have used in all examples in this series, to BabyVultures, which is the application.

    Note: If you want to add a degree of security or digital rights management (DRM) to this file, change rtmp to rtmpe and you will automatically encrypt the stream.

  5. Press the Return/Enter key twice and enter the remaining code:

    nc.onStatus = function(ncObj:Object) {
      trace(ncObj.code);
      if (ncObj.code == "NetConnection.Connect.Success") {
        ns.setBufferTime(2);
        myVideo.attachVideo(ns);
        ns.play("mp4:Vultures");
      }
    };

If you test the movie in Flash at this point (see Figure 5), the video will play. The great thing about the Flash Media Server is that it is "smart." When the connection is made, it automatically looks for a streams folder. When it finds the folder it then thinks, "Ha! The video found in the ns.play() method is in the _definst_ folder!" It immediately scoots into that folder, finds the video, and tosses it onto the stream.

The code and the application it runs

Figure 5. The code and the application it runs

Where to go from here

Now that you know how to play video through an application you create on the FMS server, you might want to expand your knowledge. The best place to start is right here by checking out these articles (currently still focused on FMS 3):

The next article in this series shows you how to encrypt your web video streams and set up SWF verification in Flash Media Server 3.5.