Ask an FMS Guru #19: I want to serve multiple FLV files from FMS2. Can this be done in a simple way?
Streaming video from FMS is probably one of the most common uses of the software. Here is a common question that I've seen:
The question: I want to serve multiple FLV files from FMS2. Can this be done in a simple way?
The answer:
A bit of background just in case.
There are two ways to stream FLV files at the moment. You can serve them from a web server (and there are a couple of ways to do this) or you can serve them from an FLV streaming server which in our case is Flash Media Server 2.0.4. Currently there are two other pieces of software out there that can stream FLV files. Red 5, which is an open source option, and Wowza which is a commercial product that works somewhat like FMS.
The difference between serving the files progressively (web server) and streaming is mainly that the file doesn't get cached on the client's computer and in theory is served faster to the client. There is a lot more information on the internet on this, so that's all I'll cover for now.
So, the idea is that we are going to use Flash and Flash Media Server to stream an FLV or multiple FLV files to our clients.
First up, let's get FMS ready to stream a file.
When installing FMS you will get a directory called "applications". It usually resides in the installation directory of FMS, but you can put it anywhere, and in fact are encouraged to move it elsewhere. But this isn't a chat on security or performance so I'll stay away from that for the time being.
In the applications directory, let's make a folder called "videostreaming". This is necessary to have clients connect up to the server itself and also a place to put our video files. Now it's important to note that you can put your videos in a central location, or multiple locations and specify those locations as virtual folders for FMS. That lets you keep videos in certain places on your server and access them from different applications. Handy.
So, we have the folder in the applications directory "videostreaming". In there, let's make a directory called "myvideos" and in there create another folder called "streams".
So:
applications-|
--------------videostreaming-|
--------------------------------myvideos-|
--------------------------------------------streams
In there, we put our video file. For this example we'll use the video file name of "video1.flv".
We have now setup FMS, let's move to Flash to build the client side.
To do this the most simple way I won't be covering controls for the video. Just how to start playing the FLV file.
Open up a new file in Flash and select the first frame and open up the actions panel.
We start off by creating a net connection to the server and then we'll watch to see when we connect up. Once connected up we will create a stream object and then play the FLV file. Playing the FLV file and showing it actually requires we make a video object, so we'll do that after the actionscript.
//create a netConnection object
var nc = new NetConnection();
//keep an eye on the status events, we need to know when we have connected up
nc.onStatus = function(info){
if(info.code == "NetConnection.Connect.Success"){
createStreamObject();
}
}
//this function is called once we have connected up to the server
function createStreamObject(){
//this is the netStream object that plays the video file
ns = new NetStream(nc);
//attach the video stream to the video object on the stage
vidObject.attachVideo(ns);
//play the video file that we put in the directory for the application
ns.play("video1");
}
//connect up
nc.connect("rtmp://MYSERVERIP/videostreaming/myvideos");
As you can see above, we are connecting to a server with the application name of "videostreaming" and the instance name is "myvideos". We put that in because that is the folder hierarchy that we created on Flash Media Server.
All that is left is to create a new layer in the timeline and make a new video object from the library panel menu. Place that on that layer on the stage and call the instance name "vidObject".
That's it! Run the file and you should have a video playing on the stage.
If it doesn't work, you need to find out if you are even connecting up to the server. That would require opening up the FMS admin panel and seeing if there are connections being made, and if there are then double check the filename and mistyping.
That is the most simplistic way to stream an FLV file. There is also the option to use the FLVPlayer component, that's a pretty good option too I think.
Posted by Graeme at March 16, 2007 10:19 AM