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

[AS3]as3开发流媒体视频播放器源代码

时间:2018-01-21 11:07酷播
[AS3]as3开发流媒体视频播放器源代码

[AS3]as3开发流媒体视频播放器源代码

  1. package 
  2.     import flash.events.*; 
  3.     import flash.media.Video; 
  4.     import flash.display.Sprite; 
  5.     import flash.net.*; 
  6.     import flash.text.TextField; 
  7.     import fl.controls.Button; 
  8.  
  9.     public class StreamingVideoPlayer extends Sprite { 
  10.         private var _videoURL:String; 
  11.         private var _video:Video; 
  12.         private var _vidDuration:Number; 
  13.         private var _vidXmax:Number; 
  14.         private var _vidYmax:Number; 
  15.         private var _vidWidth:Number; 
  16.         private var _vidHeight:Number; 
  17.         private var _main_nc:NetConnection = new NetConnection(); 
  18.         private var _serverLoc:String; 
  19.         private var _ns:NetStream; 
  20.         private var _start_btn:Button; 
  21.  
  22.         /* ------------Contructors/Initialization-----------*/ 
  23.         public function StreamingVideoPlayer(serverLoc:String, 
  24.             flvLocation:String,vidDuration:Number,vidXmax:Number, 
  25.             vidYmax:Number):void { 
  26.             //set video params 
  27.             _videoURL = flvLocation
  28.             _vidDuration = vidDuration; 
  29.             _vidXmax = vidXmax; 
  30.             _vidYmax = vidYmax; 
  31.             //add eventListeners to NetConnection and connect 
  32.             _main_nc.addEventListener(NetStatusEvent.NET_STATUS, 
  33.                 onNetStatus); 
  34.             _main_nc.addEventListener( 
  35.                 SecurityErrorEvent.SECURITY_ERROR, 
  36.                 securityErrorHandler); 
  37.             _main_nc.connect(serverLoc); 
  38.         } 
  39.         private function onNetStatus(event:Object):void { 
  40.             //handles NetConnection and NetStream status events 
  41.             switch (event.info.code) { 
  42.                 case "NetConnection.Connect.Success": 
  43.                     //play stream if connection successful 
  44.                     connectStream(); 
  45.                     break; 
  46.                 case "NetStream.Play.StreamNotFound": 
  47.                     //error if stream file not found in 
  48.                     //location specified 
  49.                     trace("Stream not found: " + _videoURL); 
  50.                     break; 
  51.                 case "NetStream.Play.Stop": 
  52.                     //do if video is stopped 
  53.                     videoPlayComplete(); 
  54.                     break; 
  55.             } 
  56.             //trace(event.info.code); 
  57.         } 
  58.  
  59.         /* -------------------Connection------------------- */ 
  60.         private function connectStream():void { 
  61.             //netstream object 
  62.             _ns = new NetStream(_main_nc); 
  63.             _ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, 
  64.                 asyncErrorHandler); 
  65.             _ns.addEventListener(NetStatusEvent.NET_STATUS, 
  66.                 onNetStatus); 
  67.             //other event handlers assigned  
  68.             //to the netstream client property 
  69.             var custom_obj:Object = new Object(); 
  70.             custom_obj.onMetaData = onMetaDataHandler
  71.             custom_obj.onCuePoint = onCuePointHandler
  72.             custom_obj.onPlayStatus = playStatus
  73.             _ns.client = custom_obj
  74.             //attach netstream to the video object 
  75.             _video = new Video(_vidXmax,_vidYmax); 
  76.             _video.attachNetStream(_ns); 
  77.             //video position could be parameterized but hardcoded 
  78.             //here to account for optional movie frame border 
  79.             _video.x = 1
  80.             _video.y = 1
  81.             addChild(_video); 
  82.             setVideoInit(); 
  83.             _ns.play(_videoURL); 
  84.         } 
  85.  
  86.         /* -------------NetStream actions and events-------------- */ 
  87.         private function videoPlayComplete():void { 
  88.             setVideoInit(); 
  89.         } 
  90.  
  91.         private function setVideoInit():void { 
  92.             _ns.play(_videoURL); 
  93.             _ns.pause(); 
  94.             _ns.seek(_vidDuration/2); 
  95.             addStartBtn(); 
  96.         } 
  97.         private function playStatus(event:Object):void { 
  98.             //handles onPlayStatus complete event if available 
  99.             switch (event.info.code) { 
  100.                 case "NetStream.Play.Complete": 
  101.                     //do if video play completes 
  102.                     videoPlayComplete(); 
  103.                     break; 
  104.             } 
  105.             //trace(event.info.code); 
  106.         } 
  107.         private function playFlv(event:MouseEvent):void { 
  108.             _ns.play(_videoURL); 
  109.             //_ns.seek(192); //used for testing 
  110.             removeChild(_start_btn); 
  111.         } 
  112.         private function pauseFlv(event:MouseEvent):void { 
  113.             _ns.pause(); 
  114.         } 
  115.  
  116.         /* ---------Buttons, labels, and other assets------------- */ 
  117.         //could be placed in it's own VideoControler class 
  118.         private function addStartBtn():void { 
  119.             _start_btn = new Button(); 
  120.             _start_btn.width = 80
  121.             _start_btn.x = (_vidXmax-_start_btn.width)/2+_video.x; 
  122.             _start_btn.y = (_vidYmax-_start_btn.height)/2+_video.y; 
  123.             _start_btn.label = "Start Video"
  124.             _start_btn.addEventListener(MouseEvent.CLICK,playFlv); 
  125.             //addChild(_start_btn); 
  126.         } 
  127.  
  128.         /* -----------------Information handlers---------------- */ 
  129.         private function onMetaDataHandler(metaInfoObj:Object):void { 
  130.             _video.width = metaInfoObj.width; 
  131.             _vidWidth = metaInfoObj.width; 
  132.             _video.height = metaInfoObj.height; 
  133.             _vidHeight = metaInfoObj.height; 
  134.             //trace("metadata: duration=" + metaInfoObj.duration +  
  135.                 //"width=" + metaInfoObj.width + " height=" + 
  136.                 //metaInfoObj.height + " framerate=" + 
  137.                 //metaInfoObj.framerate); 
  138.         } 
  139.         private function onCuePointHandler(cueInfoObj:Object):void { 
  140.             //trace("cuepoint: time=" + cueInfoObj.time + " name=" +  
  141.                 //cueInfoObj.name + " type=" + cueInfoObj.type); 
  142.         } 
  143.  
  144.         /* -----------------------Error handlers------------------------ */ 
  145.         private function securityErrorHandler(event:SecurityErrorEvent): 
  146.         void { 
  147.             trace("securityErrorHandler: " + event); 
  148.         } 
  149.         private function asyncErrorHandler(event:AsyncErrorEvent):void { 
  150.             trace(event.text); 
  151.         } 
  152.     } 

 

热门文章推荐

请稍候...

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

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