Actionscript - Video Player Help! - Flash
I've been trying for days to figure out how to get a video to play in flash and I have got pretty much nowhere. I have the code below but have no idea what else to try to get it to work. Can a开发者_开发问答nyone please help?
var conn:NetConnection = new NetConnection();
conn.connect(null);
var nstream:NetStream = new NetStream(conn);
nstream.setBufferTime(10);
trailer.attach(nstream);
nstream.play("arthur.flv");
Looks like you forgot one crutial part, you need to add the NetStream to a video object after the NetConnection has connected successfully.
var connection = new NetConnection();
connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
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;
}
}
function connectStream():void {
stream = 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);
}
Take a look at the AS3 NetStream docs here. Theres ALOT of info and examples there to get you on your way.
Is trailer added to the stage? Like this:
var trailer = new Video();
trailer.attachNetStream(nstream);
addChild(trailer);
Also have you checked that the NetStream doesn't produce an error? Like this:
nstream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
nstream.play("video.flv");
function asyncErrorHandler(event:AsyncErrorEvent):void{
trace(event);
}
Edit: Also have you check the net status and security errors? Like this:
nstream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
conn.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
conn.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
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;
}
}
function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler: " + event);
}
精彩评论