rottendevice
rottendevice

Reputation: 2879

Using the NetConnection Class

I'm trying to use the NetConnection class to connect to a live video feed on an external server. I have it set up to start playing my video when the user clicks a Play button, however, this appears in my output every time I click the Play button:

ArgumentError: Error #2126: NetConnection object must be connected. at flash.net::NetStream/ctor() at flash.net::NetStream() at Over/connectLiveStream()[Over::frame2:31]

Any ideas as to why this isn't working? Here is the (I think relevant) code:

 if (playVid.label == "Play") 
 {
  nc = new NetConnection();
      nc.objectEncoding = flash.net.ObjectEncoding.AMF0;
      nc.connect("rtmp://my.rtmp.server:1935/live/");

      nsPlay = new NetStream(nc);
      nsPlay.play("livestream.flv");

 }

Thanks in advance.

Upvotes: 2

Views: 4755

Answers (1)

SynerCoder
SynerCoder

Reputation: 12766

I am copying this from the adobe documentation site:

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

public class NetStatusEventExample extends Sprite {
    private var videoURL:String = "Video.flv";
    private var connection:NetConnection;
    private var stream:NetStream;

    public function NetStatusEventExample() {
        connection = new NetConnection();
        connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
        connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
        connection.connect(null);
    }

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

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

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

    private function asyncErrorHandler(event:AsyncErrorEvent):void {
        // ignore AsyncErrorEvent events.
    }

}
}

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

Hope this can help, and if it throws a security error you need to set a crossdomain policy file on the streaming server you connect too.

Links:

Upvotes: 1

Related Questions