AS 3 to FMS 2.0 101: objectEncoding

The way of handling videos has been changed a bit on AS3/FP9 as opposed to previously. Here are some basic notes:

1. NetConnection.defaultObjectEncoding = ObjectEncoding.AMF0 (see note below for details);

2. It’s crutial to listen to/handle “NetStatusEvent” and before playing the video:

nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);

The handler waits for == "NetConnection.Connect.Success" before calling netStream/video, etc. and subsequent actions. If you don’t wrap these actions within onNetStatus handler and just call them right after nc.connect(url), the complier will throw the following error:

ArgumentError: Error #2126: NetConnection object must be connected.
at video_AS3_02_fla::MainTimeline/video_AS3_02_fla::frame1()

So it’s basically 2-step process to code for playing prerecorded videos in FMS2/FP9.

3. Both “rtmp:/test_streams/” and “rtmp://localhost/test_streams/” work if streaming from the same machine;

4. The videos are under “Flash Media Server 2\applications\test_streams\streams\_definst_”

More about “ObjectEncoding”:
When using AS 3 in conjunction with Flash Media Server 2 (which was released prior to AS 3), it’s crucial to set “ObjectEncoding” property of the NetConnection instance or class to “AMF0” (encoding for ActionScript 1_2) using the following code; otherwise, a netconnection will fail ( “NetConnection.Connect.Failed”/”objectEncoding error”). It can be either a class property or per instance base:

NetConnection.defaultObjectEncoding = ObjectEncoding.AMF0;


nc.defaultObjectEncoding = ObjectEncoding.AMF0;

The following code snippets show how to play a prerecorded flv stream from FMS 2:

This is a basic two-step process that is needed to play flv

package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;

public class StreamTest extends Sprite
private var appURL:String = "rtmp://localhost/test_streams/";
private var nc:NetConnection;
private var trcTxt:TextField;
private var trctxtFmt:TextFormat;

public function StreamTest()
private function init():void{
trcTxt = new TextField();
trcTxt.border = true;
trcTxt.wordWrap = true;
trcTxt.autoSize = TextFieldAutoSize.LEFT;
trcTxt.width = 400;
trcTxt.height = 30;
trctxtFmt = new TextFormat();
trctxtFmt.font = "Verdana";
trctxtFmt.size = 8;
trcTxt.defaultTextFormat = trctxtFmt;

//need to set to AS2
NetConnection.defaultObjectEncoding = ObjectEncoding.AMF0;

nc = new NetConnection();
//nc.client = new CustomClient();
nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsyncError);
nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
private function connectStream():void{
var ns:NetStream = new NetStream(nc);
//ns.client = new CustomClient();
var vid:Video = new Video();
//this works
//var url:String = "";
//this doesn't work
//var url:String = "powell.flv";
//this works
//var url:String = "flv:powell";
//this works, too!
var url:String = "powell";, 0, -1, false);
trcTxt.text = vid.toString();
private function onNetStatus(evt:NetStatusEvent):void{
case "NetConnection.Connect.Success":
trcTxt.text =;
case "NetConnection.Connect.Failed":
trcTxt.text =;
case "NetStream.Play.StreamNotFound":
trcTxt.text = "not found";
private function onSecurityError(evt:SecurityErrorEvent):void{
trcTxt.text = evt.toString();
private function onAsyncError(evt:AsyncErrorEvent):void{
trcTxt.text = evt.toString();
//class CustomClient{
//public function onMetaData(info:Object):void{
//trace("metadata: duration="+info.duration+" width="+info.width+"height="+info.height+" framerate="+info.framerate);
//public function onCuePoint(info:Object):void{
//trace("cuepoint: time="+info.time+" name="" type="+info.type);


13 Responses to “AS 3 to FMS 2.0 101: objectEncoding”

  1. marsen Says:

    Thanx for example but it give alwats Connection failed.

  2. Pablo Says:


    Greate code! I got my problems solved! πŸ™‚
    One last question:

    My code is looking exactly like your and is working, but i would like to add one more thing – a FLVplayback component and a button…

    When a press the button the component should hook up and stream from the server…but a cant not make work?

    My code looks like this:

    function watchShow(event:MouseEvent):void
    myFLVPlayback_mc.isLive = true;
    myFLVPlayback_mc.source = ‘rtmp://localhost/videochat/’ + streamName_txt.text;

    NB: I’m not using FMS2 but Wowza which currently is running local…

  3. William Says:

    Thanks a lot, the

    NetConnection.defaultObjectEncoding = ObjectEncoding.AMF0;

    line saved me lot of trouble, I was going nuts! You could also mention the fact this line need to be added *before* the NetConnection object is instantiated. It makes some sens, but still not that obvious.

  4. Leland Says:

    If you want to set the objectEncoding on a per instance basis you will actually need to use the code myNetConnection.objectEncoding = ObjectEncoding.AMF0 which is the only way I could get it working.

  5. Nick Says:

    Thank you, thank you. Like William, I was going nuts with this, trying to connect FP9 to FMS2 and getting failed connections. One line and it’s working.

  6. Max Says:

    Thanks for the code, AS3 connects to FMS2 but still can’t launch the video1.flv because of this:
    Error #2044: NetStatusEvent unhandeld. level=error, code=NetStream.Play.StreamNotFound
    at StreamTest/::connectStream()
    at StreamTest/::onNetStatus()
    Is there something to do with the main.asc in StreamTest directory ? the flv is well located in /applications/StreamTest/streams/_definst_ used appURL:String = “rtmp:/StreamTest” or “rtmp://localhost/StreamTest”; “video1” as url:String


  7. Will Says:

    Many thanks for this! the defaultObjectEncoding was eluding me as well, causing much early-grey-hair growth.

    Although now I’m getting errors that the netConnection instance needs a “onBWDone” handler?

  8. casey stalnaker Says:

    Hey Will :

    Add this line to the top of your script, it will sort you out:

    NetConnection.prototype.onBWDone = function(p_bw) {
    trace(“onBWDone: “);

  9. Danilo Soares Aliberti Says:

    Thanx too much!!! This tip is simple e util. Thanx again.

    Streaming good vibes! dsa πŸ˜‰

  10. tibo Says:

    I know it was published a long time ago, but i don’t see any solution to the same problem… with AS3 flvplayback and fms2. Do you have any idea ?
    Thanks a lot,

  11. A Complete Reference for Flash Media Server (FMS). « Flashy Hawk Says:

    […] […]

  12. Hannan Azam Says:

    Thanks a lot.
    Your solution really was very much helpful.

  13. Actionscript 3.0 and FMS2 » free icons download Says:

    […] SWF file is in a network sandbox and the file to be loaded is local.||| Found the answer here: AS3 to FMS 2.0 101 Just ad the following line before you create the NetConnection instance: quote: […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: