Skip to main content
January 28, 2007
Question

actionscript 3 and rtmp

  • January 28, 2007
  • 2 replies
  • 1420 views
Hi all:

I'm trying to create a simple connection to my FMS sever using actionscript 3 and the Flash 9 Alpha. The code below *kind of works,* but I know it's wrong - I don't know where it's wrong, but I know that it's wrong. I've been googling, reading the articles here (state machine), and going through the AS3 cookbook, but I'm this is the closest I can get by myself.

Any help would be really appreciated.

package {
import flash.display.Sprite;
import flash.events.NetStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.net.defaultObjectEncoding.*;

public class VideoExample extends Sprite {
private var videoUrl:String = "1";
private var connection:NetConnection;
private var stream:NetStream;

//private function init():void{
// Create the NetConnection and listen for NetStatusEvent and SecurityErrorEvent events
//nc = new NetConnection();
//nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
//nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, netSecurityError);
//nc.connect("rtmp:/myApplication");
//}


public function VideoExample():void {
NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
connection = new NetConnection();
connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
connection.connect("rtmp://...videosource/");
}

private function netStatusHandler(event:NetStatusEvent):void {
switch (event.info.code) {
case "NetConnection.Connect.Success":
connectStream();
break;
}
}

private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler: " + event);
}

private function connectStream():void {
var stream:NetStream = new NetStream(connection);
var video:Video = new Video();
video.attachNetStream(stream);
stream.play(videoUrl);
addChild(video);
}
}
}
    This topic has been closed for replies.

    2 replies

    February 2, 2007
    try putting an onBWdone handler into the custom client:

    public function onBWDone(info:Object):void{
    }
    January 28, 2007
    Found AS3 code on the Flex site, but it throws the following errors on trace (video does stream, but this isn't working right).

    Can someone please help?

    =======

    Error #2044: Unhandled AsyncErrorEvent:. text=Error #2095: flash.net.NetConnection was unable to invoke callback onBWDone. error=ReferenceError: Error #1069: Property onBWDone not found on flash.net.NetConnection and there is no default value.
    at VideoTest$iinit()
    metadata: duration=570.2330000000001 width=796 height=536 framerate=30

    package {
    import flash.display.Sprite;
    import flash.events.NetStatusEvent;
    import flash.events.SecurityErrorEvent;
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.events.Event;
    import flash.net.ObjectEncoding;

    public class VideoTest extends Sprite{
    private var videoURL:String = "1";
    private var connection:NetConnection;
    private var stream:NetStream;

    public function VideoTest():void {
    ////NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
    connection = new NetConnection();
    connection.objectEncoding = flash.net.ObjectEncoding.AMF0;
    connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
    connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
    connection.connect("rtmp://.../videosource/");
    }



    private function netStatusHandler(event:NetStatusEvent):void
    {
    switch (event.info.code)
    {
    case "NetConnection.Connect.Success":
    connectStream();
    break;
    case "NetStream.Play.StreamNotFound":
    trace("Stream not found: " + videoURL);
    break;
    }
    }

    private function securityErrorHandler(event:SecurityErrorEvent):void
    {
    trace("securityErrorHandler: " + event);
    }

    private function connectStream():void
    {
    var stream:NetStream = new NetStream(connection);
    stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
    stream.client = new CustomClient();
    var video:Video = new Video();
    video.attachNetStream(stream);
    stream.play(videoURL);
    addChild(video);
    }
    }
    }

    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);
    }
    }