Showing posts with label AS2. Show all posts
Showing posts with label AS2. Show all posts

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






Monday, September 5, 2011

Flash Combo Box corrupted when loading

There are lot of issues with Flash AS2 components, It is all because of the uncleared global styles. Components styles are stored in a global variable, Which is not getting cleared even after removing the entire component from the SWF file.

Because of this Some time the dynamic components are displayed as corrupted. For example we can take ComboBox component. Test the following scenario in you flash file.



  1. Create a New Flash file, Drag and drop two ComboBox V2 component in to your stage and add some values to display

  2. Make one Combo enabled and other one as disabled.

  3. Save it as myCombo.fla, Now publish the file and close it.

  4. Create a New Flash file and name it ComboTest.fla and place a Button on stage, and place an empty movieclip say "content_mc" on stage.

  5. To the button Provide the functionality to unload the Existing content from content_mc and load the "myCombo.swf". The unloading is usefull to remove the existing content before loading a new one.

  6. Now Publish your ComboTest.fla file

When you run the file, it will display the Combobox element perfectly without any error. But if you unload the content and reload the SWF, Oops the disabled Combobox displayed as corrupted. The Dropdown part of the disabled combo displays as wiered boxes.

This problem is due to uncleared global Skin variables. You can test this in your Flash file by doing this simple step.


Load a Component into a MovieClip, Then remove it completely from your swf file. Now Click on "Debug" Menu button in your SWF window and select "List Variable" menu item.

It will display you a number of uncleared Variables. Few important variables are listed below
_global.mx;
_global.style;
_global.cascadingStyles;
_global.styles;
_global.skinRegistry;
_global.getStyleCounter;


To reset your skin problems you need to clear the "skinRegistry" variable to do this just delete the variable

delete _global.skinRegistry;

So, If you are facing problem with component skinning, Just delete the skinRegistery and load your component.

Thats it, it will work



Sunday, September 4, 2011

Math.random

There’s a lot of times, in programming, when you need a number that’s random, or (loosely speaking) unpredictable. For instance, if you have a game in which the throwing of dice is simulated. For that sort of need, ActionScript has a built-in constant, written exactly like so:
Math.random()
The Adobe LiveDocs give the following description of our friend Math.random():
“a pseudo-random number n, where 0 <= n < 1″
That certainly makes it clear, doesn’t it? If you’ve had maths in school, or rather, if you could remember it at all, you’d know that the Math.random() number falls within this interval:
[0, 1>
The square bracket means that 0 is in the interval, and the open bracket means that 1 is not. And that means that these are the sort of numbers Math.random() generates:
0
0.374539
0.2
0.999999999
0.45311
Et cetera. So the lowest number you could get is 0, and the highest is very close to, but never quite, 1.
How is that useful to you?
Say you need to simulate the flipping of a coin. Then you need either a 0 or a 1. How could you ever achieve that with Math.random()?
Luckily, there are 3 methods for rounding in ActionScript that you can call to the rescue. They are:
Math.ceil()
Math.floor()
Math.round()
Math.ceil() rounds numbers to the nearest higher integer.
Math.floor() rounds numbers to the nearest lower integer.
Math.round() rounds numbers to the nearest integer, whether it happens to be a higher or a lower one.
If you want to get familiar with this, do the Math.random exercise, and you'll soon feel comfortable with this stuff.
So if I want a number that's either 0 or 1, what do I use? You guessed it:
Math.round(Math.random());
Mind all the brackets! Can't afford to miss even one, or you'll get an error thrown at you.
And what do I do if I want a whole number between 0 and 10?
Well, Math.random() = a number between + including 0 and 0.99999999999 [it goes on infinitely, of course]. So if I just multiply the whole Math.random() thing by 10, I get: a number between 0 and 9.999999999. If I round this, I get a whole number between 0 and 10. So we use:
Math.round(Math.random()*10);
What if I want a whole number between 6 and 117?
Well, Math.random() = a number between + including 0 and 0.9999999999, so if I multiply Math.random() by 117 and round it, I get a whole number between 0 and 117. So that won’t work. The trick is to multiply Math.random() by (117 – 6) = 111, and then add 6! And, of course, round the whole thing again. So you get:
Math.round(Math.random()*111) + 6;
This way, ActionScript takes Math.random(), or a number between 0 and 0.9999, multiplies it by 111 to get a number between 0 and 110.9999999, then rounds those to get a number between 0 and 111, and finally adds 6 to the whole enchilada, resulting in: a number between 6 and 117.
Of course you’re not going to remember that. I would never expect you to! I’m just going to provide you with a little formula which you can look up, any time you need it, in the Very Handy Reference. Here it is:
to get a whole number between a and b, use:
Math.round(Math.random()*(b-a)) + a
-Thanks Actionsscriptmoron-