Skip to main content
October 17, 2007
Question

video class cannot work in flex, how?

  • October 17, 2007
  • 1 reply
  • 295 views
i am a flex beginner, try to use flex 2.0 to develop flv player, but it seem i cannot work. pls help me see what error inside the code.


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

public class VideoExample extends Sprite {
private var videoURL:String = "a.flv";
private var connection:NetConnection;
private var stream:NetStream;
NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;

//
public function VideoExample() {
trace("videoExample");
connection = new NetConnection();
connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
connection.connect('rtmp://localhost/sample_room/_definst_');
trace("connect");
}

private function netStatusHandler(event:NetStatusEvent):void {
trace(event.info.code);
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.
}
}
}
    This topic has been closed for replies.

    1 reply

    October 18, 2007
    Are you building a pure ActionScript 3 project or are you planning to use this in a Flex Project?

    If you are planning on using this class in a Flex project you need to import mx.core.UIComponent; and have your class extend it (UIComponent) not Sprite. Then you can import it and use it within your flex project

    If you are going to use pure ActionScript you can leave it as is.

    Also, you need a "streams" directory under you application directory ( 'sample_room\streams). This is where you FLV should go. And when you connect you should connect to "rtmp://localhost/sample_room/ " NOT "rtmp://localhost/sample_room/_definst_" FMS know to automatically look for a streams directory under the application root. One last thing tell the stream to play "a" not "a.flv". FMS knows what the extension should be.