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.