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

[AS3]as3.0中的flv视频和swf的文件截图示例代码

时间:2013-06-01 18:15CuPlayer.com
[AS3]as3.0中的flv视频和swf的文件截图示例代码,要对flv 截图,必面是下载方式,好像rtmp方式我没有试成功

要对flv 截图,必面是下载方式,好像rtmp方式我没有试成功

  1. package com.heyuansoft.image{ 
  2. import com.adobe.images.JPGEncoder; 
  3.     import com.adobe.images.BitString; 
  4.     import flash.utils.*; 
  5. import flash.display.*;    
  6.     import flash.net.*; 
  7.     import flash.events.*; 
  8.     import flash.utils.ByteArray; 
  9. import fl.video.*; 
  10.  
  11. public class CatchImageToSwf extends MovieClip{ 
  12.    private var mc1:MovieClip = new MovieClip(); 
  13.    private var ldinfo; 
  14.    private var flvplay:FLVPlayback = new FLVPlayback(); 
  15.    public function catchImageToSwf(){ 
  16.      trace("start......"); 
  17.    } 
  18.    private function trim(myString:String){ 
  19.     if(myString.indexOf(" ")==-1 && myString.indexOf(" ")==-1){ 
  20.      return myString; 
  21.       } 
  22.      var omyString:String=myString; 
  23.      myString=replace(myString," "," "); 
  24.      var lblanknum:Number=0
  25.      var rblanknum:Number=0
  26.     for(var i:Number=0;i<myString.length;i++){ 
  27.      if(myString.charAt(i)!=" "){ 
  28.          break; 
  29.      }else{ 
  30.         lblanknum++; 
  31.      } 
  32.      } 
  33.     for(var j:Number=(myString.length-1);j>0;j--){ 
  34.      if(myString.charAt(j)!=" "){ 
  35.          break; 
  36.      }else{ 
  37.          rblanknum++; 
  38.      } 
  39.      } 
  40.     return omyString.substring(lblanknum,(omyString.length-rblanknum)); 
  41. private function replace(s:String,o:String,n:String){ 
  42.     return ss=s.split(o).join(n); 
  43.    } 
  44.    private function getFileName(URLpath:String){ 
  45.     return URLpath.substring(URLpath.lastIndexOf("/")+1,URLpath.length); 
  46.    } 
  47.    private function getExpendName(URLpath:String){ 
  48.     var fileName = getFileName(trim(URLpath)); 
  49.     if(fileName.indexOf(".")==-1){ 
  50.     return null; 
  51.     }else{ 
  52.     var temp:String =fileName.substring(fileName.lastIndexOf(".")+1,fileName.length); 
  53.     return temp; 
  54.          } 
  55.    } 
  56.    private function getError(e:Event):void{ 
  57.      trace("error......"); 
  58.    } 
  59.    public function setMc1WidthAndHeight(w:Number,h:Number){ 
  60.     mc1.width = w; 
  61.     mc1.height = h; 
  62.    } 
  63.    public function setFlvPlayWidthAndHeight(w:Number,h:Number){ 
  64.     flvplay.width=w; 
  65.     flvplay.height=h 
  66.    } 
  67.    public function getImage(){ 
  68.     var expendName = getExpendName(url); 
  69.     switch(expendName){ 
  70.      case null:trace("typeError"); 
  71.      case "flv": playFlv(url,quality,serApp,imageName);break; 
  72.      case "swf": playSwf(url,quality,serApp,imageName);break; 
  73.      
  74.     } 
  75.    } 
  76.    private function playFlv(flvUrl:String,quality:Number,serApp:String,imageName:String):void{ 
  77.  
  78.     flvplay.source=flvUrl
  79.     flvplay.addEventListener("progress",printinfo); 
  80.     function printinfo(infoObj:VideoProgressEvent):void{ 
  81.      if(infoObj.bytesLoaded == flvplay.bytesTotal){  
  82. //这里要注意的就是不须要这么久, 
  83. //可以下载到二分之一处就行,因为我只要在三分之一的地方截图 
  84.      trace("source : "+flvplay.bytesTotal); 
  85.      trace(Math.floor(flvplay.totalTime/3)); 
  86.     flvplay.seekSeconds(Math.floor(flvplay.totalTime/3)); 
  87.      } 
  88.     } 
  89.     flvplay.addEventListener("seeked",compSeek); 
  90.     function compSeek(e:Event){ 
  91.      trace("seeked"); 
  92.      
  93.      createJpg(flvplay,quality,serApp,imageName); 
  94.       flvplay.stop(); 
  95.     } 
  96.     
  97.    } 
  98.    
  99.    private function playSwf(swfUrl:String,quality:Number,serApp:String,imageName:String):void{ 
  100.     var myVar:int = Math.floor(Math.random()*1000000); 
  101.     var url_img:URLRequest = new URLRequest(swfUrl+"?number="+myVar); 
  102.     var ld:Loader = new Loader(); 
  103.     ldldinfo=ld.contentLoaderInfo; 
  104.     ldinfo.addEventListener(Event.COMPLETE,getinfo); 
  105.     function getinfo(e:Event):void{ 
  106.       try{ 
  107.           mc1 =ldinfo.content; 
  108.        mc1.gotoAndStop(Math.ceil(mc1.totalFrames/3)); 
  109.         createJpg(mc1,quality,serApp,imageName); 
  110.          }catch(error:TypeError){ 
  111.        mc1.addChild(ld); 
  112.        var minuteTimer:Timer = new Timer(3000, 1); 
  113.          minuteTimer.start(); 
  114.                   minuteTimer.addEventListener(TimerEvent.TIMER, onTick); 
  115.        function onTick(event:Event):void 
  116.               { 
  117.         createJpg(mc1,quality,serApp,imageName); 
  118.        } 
  119.       } 
  120.       } 
  121.     ldinfo.addEventListener("ioError" ,getError); 
  122.     ld.load(url_img); 
  123.    } 
  124.    private function createJpg(object:DisplayObject,quality:Number,serApp:String,imageName:String):void{ 
  125.     trace("xxx"+mc1.height); 
  126.     if(object.width!=0||object.height!=0){ 
  127.     var jpgBd:BitmapData = new BitmapData(object.width,object.height); 
  128.     jpgBd.draw(object); 
  129.     var jpgEncoder:JPGEncoder = new JPGEncoder(quality); 
  130.     var jpgStream:ByteArray = jpgEncoder.encode(jpgBd); 
  131.     trace("jpgStream:"+jpgStream.length); 
  132.     var header:URLRequestHeader = new URLRequestHeader("Content-type","application/octet-stream"); 
  133.     var url:String =serApp+imageName; 
  134.     trace(url); 
  135.     var jpgURLRequest:URLRequest = new URLRequest (url);   
  136.     jpgURLRequest.requestHeaders.push(header);     
  137.     jpgURLRequest.method = URLRequestMethod.POST;     
  138.     jpgURLRequest.data = jpgStream
  139.     //navigateToURL(jpgURLRequest); 
  140.     var myLoader:URLLoader = new URLLoader(jpgURLRequest); 
  141.     } 
  142.    } 

 

热门文章推荐

请稍候...

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

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