Friday, September 9, 2011

How to connect with Flash media server - For beginner

What is FLASH MEDIA SERVER ?


Flash media server is one of the best server created to connect multiple clients in to a single virtual world. Flash media server used for many reasons, few of them as follows

1. For Video live telecast
2. For Video live recording & Telecasting
3. For Online Video Broadcasting with DRM in a secured way
3. For Online Chatting application
4. For Online Video Conferencing
5. For Online Multi-user Application or Multi-Player game development
and many more

How to install and Where to install Flash Media server ?


For testing purpose you can install the server in your local machine, But if you whish you access using Internet from other machines, then you need to install it in a dedicated webserver machine. Now a days many hosting sites like www.influxis.com providing you shared media server access with less cost. Please note, Better to install the media server in the higher bandwidh system, So that it can serve better even the load is heavy.

Setup the connection folder


Before creating our flash coding, we need to setup the application folder to connect with. For that Copy server-side script files for an application to the folder you registered on the server. For example, for an application called "videoPlayer", copy the main.asc file to RootInstall/applications/videoPlayer. You can also place server-side scripts in "scripts" subfolder. For example, you can use either of these locations:

RootInstall/applications/appName

RootInstall/applications/appName/scripts

Note: To replace a running application, copy the new files, then use the Administration Console to restart the application.

Sample Main.asc code :



application.onConnect = function (client1){


application.acceptConnection(client1);


client1.call("welcome");


};


Copy media files to the server

Copy video and audio files to the streams/_definst_ folder in the application folder:

RootInstall/applications/appName/streams/_definst_/

If an application connects to an instance of the application, for example, nc.connect("rtmp://fms.example.com/appName/someInstance"), place the streams in the following folder:

RootInstall/applications/appName/streams/someInstance/

There are several ways to configure the server to look for media files stored in other locations. See the Adobe links for more information


How to connect using RTMP


RTMP is the protocol used by Adobe’s Flash Media Server to stream content into flash. Most of the help documentation doesn’t touch much on this method of connection and is limited to sources not easily available to users.


This article should be used by those with an understanding of video playback using actionscript 2. If that isn’t you, please read this article before continuing.

1.) In order to use the RTMP protocol, you will need to install and run a version of Adobe Flash Media Server. Adobe has released a free developer version the limits you to 10 connections. That will be MORE than enough to do some basic development. Please make sure you are running or have access to a Flash Media Server before continuing to the next step.

2.) Now that you have access to a Flash Media Server, we will start with some basic code that should look familiar if you have worked with actionscript and video before. For this step, you will need to know the URI for the server. If you installed Flash Media server on the machine you are running on, you can use "localhost" for the URI. We will be using the "vod" app that comes pre-installed on the server for this example.

var nc:NetConnection = new NetConnection();
nc.connect("rtmp://YOUR_SERVER_URI/vod/");

Note : If we were loading an flv file without streaming, you would have passed null to the connect() method instead.

3.) The only thing left is to load the stream, instead of loading an FLV video from a URL. This is actually much easier than you think. Instead of passing a URLRequest to the NetStream.play() method, you would instead pass the name of the stream you wish to play from within your app (this is the name of the FLV file on the server, but without the .FLV extension). To playback an HD streaming file, you will need to format your stream name slightly different: "mp4:NAME_OF_STREAM.mp4", "mp4:NAME_OF_STREAM.m4v"

Please note that you can not setup your NetStream until after the NetConnection has successfully connected. You will know this from the NetStatus event: NetConnection.Connect.Success that is dispatched.

var ns:NetStream = new NetStream(nc);
ns.play("NAME_OF_STREAM");

// ns.play("NAME_OF_FLV"); // No Suffix or prefix required for FLV file format
// ns.play("mp4:NAME_OF_STREAM.mp4"); // this will stream an HD movie instead.
// ns.play("mp4:NAME_OF_STREAM.m4v"); // this will stream an HD movie instead.
// ns.play("mp3:Audio_File_Name"); // No .mp3 suffix, to play MP3 files


SAMPLE RTMP VIDEO PLAY BACK USING FLASH AS2


var my_nc:NetConnection = new NetConnection()
my_nc.connect("rtmp://localhost/vod");

var my_ns:NetStream = new
NetStream(my_nc);

my_nc.onStatus = function(info:Object){
trace("[
NET CONNECTION STATUS ] :: " + info.code);
if(info.code ==
"NetConnection.Connect.Success"){
my_ns.setBufferTime(3);
//Here
my_video is the Video component created from Library
my_video.attachVideo(my_ns);
my_ns.play("SampleFLV");
//my_ns.play("mp4:SampleHDVideo");
//my_ns.play("mp3:SampleMP3File");
//my_ns.play("mp4:SampleM4VFile.m4v");
}
}

This code is applicable for WOWZA Media server too. Connection method is same for FMS and WOWZA Media Server