12 January 2009
Beginning
This article is the third in my series of beginning-level Flash Media Server 3.5 tutorials. This particular tutorial shows you how to use ActionScript 3.0 to stream an H.264 video from the vod folder, and then stream that same video from an application.
Here are all the articles in the series:
First, I'll show you how to stream H.264 video from the vod folder using ActionScript:
Nothing new here. If you have been following this series, you know the vod folder is seen as a publishing point. To date, all we have done is to simply set the Content Path of the FLVPlayback component to rtmp://localhost/vod/mp4:Vultures and the video plays.
In this tutorial we are going to skip using the FLVPlayback component and, instead, feed the video from the vod folder into a video object on the Stage. Here's how:
Note: The video object you use should match the dimensions and/or aspect ratio of the FLV or MP4 file it plays. The default size for a video object on the stage is 160 × 120. This is a 4:3 aspect ratio and works with FLV files that use this ratio. If you are using HD content that uses the common 16:9 aspect ratio, failing to resize the video object will result in a distorted video when it appears in the video object.
var nc:NetConnection = new NetConnection();
nc.connect("rtmp://localhost/vod");
var ns:NetStream = new NetStream(nc);
The first line sets up the NetConnection and the second line tells Flash the NetConnection is to the vod folder in FMS 3.5. If you have been using a video object to progressively download a file, you will notice the connection string is the path to the vod folder on the FMS 3.5 server and not a "null" string. In this example, you are working locally with the developer version of FMS 3.5. If you have a commercial account, localhost will be replaced with an actual RTMP address (which is a series of numbers) and there might, or might not, be a path to the vod folder, depending upon your ISP's unique requirements.
With the connection made and the NetStream established, it's time to do a little bit of housekeeping and make sure you have successfully connected to the server—also to increase the amount of the video that will be buffered before it streams. Here's how:
nc.onStatus = function(ncObj:Object) {
trace(ncObj.code);
if (ncObj.code == "NetConnection.Connect.Success") {
ns.setBufferTime(2);
The code starts by using an onStatus event to check if you have successfully connected to FMS 3.5. You do this by by capturing the code string ("NetConnection.Connect.Success") sent to Adobe Flash Player when the SWF connects to FMS 3.5. The trace statement will open the Output panel and actually display that code (see Figure 1) if there has been a successful connection.
If the connection is successful, the setBufferTime() method of the NetStream class is used to put two seconds of video into the buffer. The default buffer value is 0.1 seconds; it's up to you how big a buffer to set.
myVideo.attachVideo(ns);
ns.play("mp4:Vultures");
}
};
The first line attaches the NetStream to the video object with the instance of myVideo on the Stage. The second uses the NetStream play() method to actually play the video. It is important to note that you need to include the media type, mp4: (including the colon), in the string. Also, because you are using FMS 3.5, you don't need to add the extension to the video's name.
Note: If you were using an FLV file instead, the play string would be "Vultures". You don't need to add the media type for FLV files.
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:
Note: I would like to thank my colleague Lisa Larson-Kelley for the use of this particular main.asc file.
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.
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.
Now that you know how these things are structured, it's time to put your new knowledge to good use and stream the video:
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.
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.
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.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
Tutorials & Samples |