Flash Media Server: Stream a Video

0

So one of the things I've really missed about not having a blog is the fact that as I've learn stuff ive not been able to pass it on and hopefully help someone else out so they dont have to search the internet with what I have found out!

Today I'm going to run through a very simple bit of code in actionscript 3 that will connect to a flash media server and then stream a video that we know is within our application!

Ok so the first thing that we need to do is to start our connection to the server, for this you need to know the address of the server you are going to use!

var nc:NetConnection = new NetConnection(); var path:String = "rtmp://localhost/vod/petesVideo"; 

So in the above code we simply initiate a new NetConnection variable and a String variable which will hold the path to our application on our server. The path to the server should take the following format:

rtmp://[path to server]/[application name]/[application instance]

When creating a connection to the server your application must exist on the server, this can be as simple as creating a folder in your application folder on your server, please refer to the instalation instructions from when you installed the media server if you are not sure where this is! The instance of the application does not have to exist when you are connecting to your application as this will be generated on the fly, however when you are streaming a video obviously this should exist!

nc.addEventListener(NetStatusEvent.NET_STATUS, ncStatus);

So the next thing we need to do is to add a listener to the NetConnection variable to be able to tell when we have a successful connection to the server, which in turn will allow us to stream the video from the server. We must have a successful connection to the server before we can initiate the stream. As we can see when the connection status changes the above line of code will call the function "ncStatus". This function is show below!

function ncStatus(event:NetStatusEvent) {     switch(event.info.code) {
        case "NetConnection.Connect.Success" : var vid:Video = new Video();
            var ns:NetStream = new NetStream(nc);
            ns.play("flvfilename");
            vid.attachNetStream(ns);
            addChild(vid);
            break;
    }
}

So breaking this function down:

function ncStatus(event:NetStatusEvent) { }

This is the wrapper for our function, whenever a function is being called from an event listener, as in this case, we need to add a variable which will pass all the information from the event into the function and dataType it with what type of event it should expect, in this case it is the NetStatusEvent and we pass it into a variable called event which we can use in the function.

switch(event.info.code) {
        case "NetConnection.Connect.Success" : break; }

We then use the switch, case event to detect what state the server is in! This is passed back in the event information so to access it we use the line event.info.code! This goes through the event object that is passed back through the listener with the code in it. When we have a successful code it will pass back the string "NetConnection.Connect.Success" so when we have this we can go ahead and create our stream for the video.

var vid:Video = new Video();
var ns:NetStream = new NetStream(nc);
ns.play("flvfilename");
vid.attachNetStream(ns);
addChild(vid);

So once we know we have the successful connection, we create a video instance, this is what will actually display the video. We create a new NetStream, and use our NetConnection instance to connect the stream to the correct place! Once we have connected the NetStream we can tell it to play the video on the server, all we need to do is pass the name of the video we have on our server to it, we DO NOT need to file extension as this is persumed to be .flv!

We then attach our stream to the video component using the Video.attachNetStream and pass it our NetStream object!

The last thing to do in this function is to add the video to the stage so we can see it! This is done using the addChild() method and passing it our video component.

nc.connect(path);

Then all we need to do to get a working application is to connect our NetConnection to the server by making the call to connect.

I will attach the fla for this shortly. Hope this helps someone out there, I'll be doing more on using the flash media server as I learn more.

The first post!

0

 The first post in a new blog is always filled with mine fields, what do you put in it, should it reflect where you've been, or where your going?! I  guess I should start with a disclaimer, most of this site as of 15:04 does not work! This site compared to the previous one is a total rewrite! I have abandoned the crapness of asp and access databases and moved into this century so this site is now written in php using mySQL for the database! Because of this I have also had to rewrite the entire content management system for the website but thats for another entry as I will want to go into far too much detail for this entry!

I never thought I would miss having a blog to write on but since I decided to retire the old website I have forced myself to not add anything new to it so I can concentrate on this version. For those of you who have just found this website, I'm Pete, originally from Peterborough, UK but currently studying for a degree in digital art and technology. I spent the whole of last year working for a company called bluestone creative who do most of their work trading as e4education. Whilst there I started the year off as a content monkey, getting the school websites live by adding content to the website that the school would provide whilst acting as the primary contact after the design process for both new and existing clients providing support via phone and email. Looking back on this time at the company now I believe it was invaluable experience to get to know how the company worked and to develop my personal skills by talking to the clients on a daily basis. Quickly I progressed into the development team where I was given the time to develop the skills I have a keen interest in, these being flash, javascript and RIA's (rich internet applications) in general. Over time I will add some of the projects I worked on during my time with the company to the work section of this site. A couple I am most proud of is the video wall for the Thomas Deacon Academy which they liked to much it is now a large part of the home page of the website. It pulls in all the videos that they have uploaded to a particular folder and then displays all the thumbnails for the videos, and if one is selected it is streamed off the server. Another major project I worked on was a podcast player which is now integrated into every website the company builds as standard. It is totally themable allowing the designers to create bespoke skins for each school or select a pre designed from a list.

I hope to further my knowledge of these areas in my final year of university trying to find an angle in every project to improve my skills in these areas. I hope to also post some tutorials on the site or hints and tips which i hope will help someone save some time!