package { import flash.display.MovieClip; import flash.net.NetStream; import flash.net.NetConnection; import flash.media.SoundTransform; import flash.media.Video; import flash.events.Event; import flash.events.MouseEvent; import flash.utils.ByteArray; import JPGEncoder.JPGEncoder; import flash.net.FileReference; import flash.display.BitmapData; import flash.geom.Rectangle; import flash.events.AsyncErrorEvent; public class VideoMain extends MovieClip { private var netStrem:NetStream; private var netconnection:NetConnection; private var soundtransform:SoundTransform; private var flag:int =1; private var video:Video; private var shichang:Number = 0; public function VideoMain() { initbar(); init(); getDispatchar(); } private function initbar():void{ _bar1.scaleX =0; _bar.scaleX = 0; } private function init():void{ //NetStream 类提供了一些方法和属性,它们可用于跟踪文件的加载和播放进度,以及便于用户对回放进行控制(停止或暂停等)。 netconnection = new NetConnection(); //NetConnection 类在 Flash Player 和 Flash Media Server 应用程序之间或者 Flash Player 和运行 Flash Remoting //的应用程序服务器之间创建双向连接 netconnection.connect(null);//传空参数 netStrem = new NetStream(netconnection); netStrem.play('1.flv'); /// play_stop.buttonMode = true; stop_btn.buttonMode = true; jp.buttonMode = true; bar.bar1.buttonMode = true; //Video 类在应用程序中显示实时视频或录制视频,而无需在 SWF 文件中嵌入视频。 video =new Video(); video.x = 3; video.y = 3; //指定要在应用程序中 Video 对象的边界内显示的视频流。 video.attachNetStream(netStrem); addChild(video); var obj:Object= new Object(); //在 Flash Player 接收到正在播放的视频中嵌入的描述性信息时建立侦听器进行响应。 obj.onMetaData = onHandler; //指定对其调用回调方法以处理流或 FLV 文件数据的对象。 netStrem.client = obj; //netStrem.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onMeta);//AsyncErrorEvent.ASYNC_ERROR常量定义 asyncError 事件对象的 type 属性值。 soundtransform = new SoundTransform(); addEventListener(Event.ENTER_FRAME,on_barHandler); addEventListener(Event.ENTER_FRAME,on_bar1eHandler); } private function on_barHandler(e:Event):void { _bar.scaleX = netStrem.bytesLoaded/netStrem.bytesTotal; if(_bar.scaleX == 1) { removeEventListener(Event.ENTER_FRAME,on_barHandler); } } private function on_bar1eHandler(e:Event):void { _bar1.scaleX = netStrem.time / shichang; } private function getDispatchar():void{ //在文件中搜索到给定时间,以秒为单位指定,精确到小数点后三位(毫秒)。 netStrem.seek(0); //暂停或恢复流的回放 netStrem.togglePause(); play_stop.addEventListener(MouseEvent.CLICK,onPlay_stopHandler); stop_btn.addEventListener(MouseEvent.CLICK,onStop_btnHandler); jp.addEventListener(MouseEvent.CLICK,onJpHandler); bar.bar1.addEventListener(MouseEvent.MOUSE_DOWN,onDownHandler); bar.bar1.addEventListener(MouseEvent.MOUSE_UP,onUpHandler); } private function onPlay_stopHandler(e:MouseEvent):void{ netStrem.togglePause(); if(flag == 1) { flag = 2; play_stop.gotoAndStop(flag); } else { flag = 1; play_stop.gotoAndStop(flag); } } private function onHandler(obj:Object):void { shichang = obj.duration;//以帧或秒为单位的补间动画的持续时间 } private function onDownHandler(e:MouseEvent):void{ bar.bar1.startDrag(true, new Rectangle(0, 0, bar.bar2.width, 0)); } private function onUpHandler(e:MouseEvent):void{ bar.bar1.stopDrag(); if (bar.bar1.x > bar.bar2.width) { bar.bar1.x = bar.bar2.width; } soundtransform = new SoundTransform(); soundtransform.volume = bar.bar1.x / bar.bar2.width; netStrem.soundTransform = soundtransform; } private function onStop_btnHandler(e:MouseEvent):void{ flag = 1; netStrem.pause(); netStrem.seek(0); play_stop.gotoAndStop(flag); } private function onJpHandler(e:MouseEvent):void{ var bitmapdata:BitmapData = new BitmapData(video.width, video.height); bitmapdata.draw(video); var jpgEncode:JPGEncoder = new JPGEncoder(); var bytesArr:ByteArray=ByteArray(jpgEncode.encode(bitmapdata)); var file:FileReference = new FileReference(); file.addEventListener(Event.SELECT, onselect); file.addEventListener(Event.CANCEL, oncancel); file.save(bytesArr, "未命名.jpg"); } private function onselect(e:Event):void{ trace("图片已保存"); } private function oncancel(e:Event):void{ trace("你取消了保存"); } } }