·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]创建 Video 和 NetStream 对象并调用 Video.attachNetStrea

时间:2016-08-18 08:49酷播
[AS3]创建 Video 和 NetStream 对象并调用 Video.attachNetStream()

创建 Video 和 NetStream 对象并调用 Video.attachNetStream()NetStream.play() 的代码放入处理函数。仅在试图连接 NetConnection 对象成功时调用处理函数,即,当 netStatus 事件返回一个 info 对象,该对象带有一个表示成功的 code 属性时,才调用该处理函数。建议先等待连接成功,然后再调用 NetStream.play()

  1. package { 
  2.    import flash.display.Sprite; 
  3.    import flash.events.*; 
  4.    import flash.media.Video; 
  5.    import flash.net.NetConnection; 
  6.    import flash.net.NetStream; 
  7.  
  8.    public class VideoExample extends Sprite { 
  9.        private var videoURL:String = "Video.flv"
  10.        private var connection:NetConnection; 
  11.        private var stream:NetStream; 
  12.  
  13.        public function VideoExample() { 
  14.            connection = new NetConnection(); 
  15.            connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); 
  16.            connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); 
  17.            connection.connect(null); 
  18.        } 
  19.  
  20.        private function netStatusHandler(event:NetStatusEvent):void { 
  21.            switch (event.info.code) { 
  22.                case "NetConnection.Connect.Success": 
  23.                    connectStream(); 
  24.                    break; 
  25.                case "NetStream.Play.StreamNotFound": 
  26.                    trace("Unable to locate video: " + videoURL); 
  27.                    break; 
  28.            } 
  29.        } 
  30.  
  31.        private function connectStream():void { 
  32.            stream = new NetStream(connection); 
  33.            stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); 
  34.            stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler); 
  35.            var video:Video = new Video(); 
  36.            video.attachNetStream(stream); 
  37.            stream.play(videoURL); 
  38.            addChild(video); 
  39.        } 
  40.  
  41.        private function securityErrorHandler(event:SecurityErrorEvent):void { 
  42.            trace("securityErrorHandler: " + event); 
  43.        } 
  44.         
  45.        private function asyncErrorHandler(event:AsyncErrorEvent):void { 
  46.            // ignore AsyncErrorEvent events. 
  47.        } 
  48.    } 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器