
I currently work on a simple VideoObject class. This class extends Sprite. The constructor takes 3 parameters: the filename of a .flv, width and height of the video. The class does all the netConnect and netStream stuff to load the video and add it to its displayList.
My goal is that I can finally simply create a new VideoObject wherever I want, supply the 3 parameters and get the reference to a sprite containing a connected, streaming and paused video.
Then I could for example place the VideoObject somewhere on the stage and resume playing or seek or whatever.
I hope that’s understandable so far.
Basically my class works. But I still encounter a bigger and a minor problem:
The bigger problem:
As you can see in the code below, I create a netStream inside my VideoObject class called ns as a public property.
But when I try to access the netStream Object through an instance of my VideoObject, I get an error telling me that I try to access a null object reference. Tracing myVideoObject.ns returns null as well.
Now what could be the problem? I mean, I should be able to access the netStream object from outside the class as well, right?
The smaller problem:
I try to dispatch a custom event when the video is ready and listen for the event from outside the class (see .fla code). The event gets dispatched but so far I have not been able to receive it.
Any ideas would be highly appreciated!
Here comes the class:
package { import flash.display.Sprite; import flash.events.Event; import flash.events.NetStatusEvent; import flash.events.SecurityErrorEvent; import flash.events.AsyncErrorEvent; import flash.media.Video; import flash.net.NetConnection; import flash.net.NetStream; public class VideoObject extends Sprite { public var videoURL:String; public var videoWidth:int; public var videoHeight:int; public var ns:NetStream; public var video:Video; private var nc:NetConnection; private var myEvent:Event; public function VideoObject(videoURL:String,videoWidth:int,videoHeight:int) { this.videoURL = videoURL; this.videoWidth = videoWidth; this.videoHeight = videoHeight; init(); } // //initialize private function init():void { myEvent = new Event(“video_loaded”); nc = new NetConnection(); nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); nc.connect(null); } // //check network connection and proceed if successfull 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; } } // //begin video streaming private function connectStream():void { var ns:NetStream = new NetStream(nc); ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler); var video:Video = new Video(); video.attachNetStream(ns); ns.play(videoURL); ns.pause(); video.width = videoWidth; video.height = videoHeight; video.x = -videoWidth/2; video.y = -videoHeight/2; addChild(video); dispatchEvent(myEvent); } // //handle security errors private function securityErrorHandler(event:SecurityErrorEvent):void { trace(“securityErrorHandler: “ + event); } // //handle async error private function asyncErrorHandler(event:AsyncErrorEvent):void { // ignore AsyncErrorEvent events. } } }
and this is the fla where I create a VideoObject instance and listen for my event:
var myVideo:VideoObject = new VideoObject(“test.flv”,720,405); myVideo.x = stage.stageWidth/2; myVideo.y = stage.stageHeight/2 addChild(myVideo); trace(myVideo.ns); myVideo.ns.resume(); myVideo.addEventListener(“video_loaded”, onVideoLoaded); function onVideoLoaded(event) { trace(“video loaded”); }
If you would like to make a comment, please fill out the form below.
Recent Comments