AS 3 to FMS 2.0 101: objectEncoding
April 20, 2007The 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 evt.info.code == "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 flash.net::NetStream/flash.net:NetStream::construct()
at flash.net::NetStream$iinit()
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 (NetStatusEvent.info.code: “NetConnection.Connect.Failed”/NetStatusEvent.info.description:”objectEncoding error”). It can be either a class property or per instance base:
NetConnection.defaultObjectEncoding = ObjectEncoding.AMF0;
or
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.net.NetConnection;
import flash.net.ObjectEncoding;
import flash.events.Event;
import flash.events.NetStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.events.AsyncErrorEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.net.NetStream;
import flash.media.Video;
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()
{
init();
}
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;
addChild(trcTxt);
//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);
nc.connect(appURL);
}
private function connectStream():void{
var ns:NetStream = new NetStream(nc);
//ns.client = new CustomClient();
var vid:Video = new Video();
vid.attachNetStream(ns);
//this works
//var url:String = “http://www.helpexamples.com/flash/video/cuepoints.flv”;
//this doesn’t work
//var url:String = “powell.flv”;
//this works
//var url:String = “flv:powell”;
//this works, too!
var url:String = “powell”;
ns.play(url, 0, -1, false);
addChild(vid);
trcTxt.text = vid.toString();
}
private function onNetStatus(evt:NetStatusEvent):void{
switch(evt.info.code){
case “NetConnection.Connect.Success”:
trcTxt.text = evt.info.code;
connectStream();
break;
case “NetConnection.Connect.Failed”:
trcTxt.text = evt.info.code;
break;
case “NetStream.Play.StreamNotFound”:
trcTxt.text = “not found”;
break;
}
}
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=”+info.name+” type=”+info.type);
//}
//}
}