Actionscript - 视频播放器帮助! - 闪光灯

时间:2011-05-02 22:52:57

标签: flash actionscript video-streaming netstream

我已经尝试了几天来弄清楚如何在Flash中播放视频而且我几乎无处可去。我有下面的代码,但不知道还有什么可以尝试让它工作。有人可以帮忙吗?

var conn:NetConnection = new NetConnection();
conn.connect(null);

var nstream:NetStream = new NetStream(conn);
nstream.setBufferTime(10);

trailer.attach(nstream);
nstream.play("arthur.flv");

2 个答案:

答案 0 :(得分:1)

拖车是否已加入舞台?像这样:

var trailer = new Video(); 
trailer.attachNetStream(nstream); 
addChild(trailer); 

您是否检查过NetStream不会产生错误?像这样:

nstream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler); 
nstream.play("video.flv"); 
function asyncErrorHandler(event:AsyncErrorEvent):void{ 
    trace(event);
} 

编辑:还要检查网络状态和安全错误吗?像这样:

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

答案 1 :(得分:1)

看起来你忘记了一个重要的部分,你需要在NetConnection成功连接后将NetStream添加到视频对象。


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

查看AS3 NetStream文档here。大量的信息和示例可以帮助您顺利前往。