0

几天来,我一直在尝试弄清楚如何让视频在 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");
4

2 回答 2

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);
}
于 2011-05-02T22:56:38.177 回答
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 文档。那里有很多信息和示例可以帮助您上路。

于 2011-05-02T23:02:21.227 回答