不够优秀的代码:
- /**
- *
- * ActionScript 3.0 需要多个类同时工作来载入和回放Flash视频。你必须使用NetStream对象载入视
- * 频并控制回放,但是NetStream类只关心如何读取数据,至于这些数据是什么内容并不知道,因
- * 此就需要Video对象,Video对象得到NetStream的数据并显示到屏幕上。
- *
- *
- * NetStream构造函数需要一个NetConnection对象作为参数,NetConnection对象关联将要播放的视频
- *
- *
- */
- package tlg.tool
- {
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.MouseEvent;
- import flash.events.NetStatusEvent;
- import flash.geom.Rectangle;
- import flash.media.Video;
- import flash.net.NetConnection;
- import flash.net.NetStream;
- import flash.text.TextField;
- import flash.text.TextFieldAutoSize;
- import tlg.customEvent.SenceChooseInfoEvent;
- public class PlayVideo extends Sprite {
- //
- public var _stream:NetStream;
- //
- private var _video:Video;
- //
- private var connection:NetConnection;
- public var _duration:uint;
- // 视频链接地址
- public var vidoUrl:String;
- private var _scrubbing:Boolean;
- private var _track:Sprite;
- private var _thumb:Sprite;
- public function PlayVideo( )
- {
- _video = new Video(160, 120);
- addChild(_video);
- _duration = 0;
- connection= new NetConnection( );
- connection.connect(null);
- _stream = new NetStream( connection );
- _stream.play( vidoUrl );
- var client:Object = new Object( );
- // 使用回调函数
- client.onMetaData = onMetaData;
- client.onCuePoint = onCuePoint;
- _stream.client = client;
- // ----------
- _video.attachNetStream( _stream );
- _stream.addEventListener(NetStatusEvent.NET_STATUS, statusHandler);
- //------------------------------ 添加控制条 ---------------------
- if ( _track == null ){
- videoScrubber ( );
- }
- }
- private function onMetaData(data:Object):void {
- _duration = int ( data.duration );
- // 输出影片 原数据
- /*var key:String;
- for (key in data)
- {
- trace(key + ": " + data[key]);
- } */
- }
- // -------------------------------- 提示点 -----------------------------------
- private function onCuePoint( cuePoint:Object ):void {
- trace(cuePoint.name + " " + cuePoint.time);
- }
- public function killVideo ( ):void {
- _video.clear( );
- //removeChild(_video);
- }
- private function statusHandler(event:NetStatusEvent):void
- {
- switch (event.info.code)
- {
- case "NetStream.Play.Start":
- //dispatchEvent(new SenceChooseInfoEvent( "videoStart"));
- break;
- case "NetStream.Play.Stop":
- killVideo ( );
- //dispatchEvent(new SenceChooseInfoEvent( "videoOver"));
- break;
- }
- }
- //--------------------------------- 添加控制滚动条 ----------------------------------
- private function videoScrubber ( ):void {
- // 滑块条
- _track= new Sprite( );
- _track.graphics.lineStyle(1,0xcccccc );
- _track.graphics.drawRect(0, -2.5,150, 5);
- _track.graphics.endFill();
- _track.y = _video.height + 20;
- this.addChild(_track);
- // 滑块
- _thumb = new Sprite( );
- _thumb.graphics.lineStyle( );
- _thumb.graphics.beginFill(0x000000);
- _thumb.graphics.drawRect(-5, -5, 10, 10);
- _thumb.graphics.endFill( );
- _thumb.y = _track.y ;
- this.addChild(_thumb);
- this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
- _thumb.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
- _thumb.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
- }
- private function onMouseDown(event:MouseEvent):void {
- _scrubbing = true;
- var rectangle:Rectangle = new Rectangle(0, 0, _track.width, _thumb.y);
- _thumb.startDrag(false, rectangle);
- }
- private function onMouseUp(event:MouseEvent):void {
- _scrubbing = false;
- _thumb.stopDrag( );
- }
- private function onEnterFrame(event:Event):void {
- if(_duration > 0) {
- if(_scrubbing) {
- _stream.seek(_thumb.x * Math.round(_duration) / 150);
- }
- else {
- _thumb.x = _stream.time / Math.round(_duration) * _track.width;
- }
- }
- }
- //--------------
- }
- }
需要一个引用这个PlayVideo 的类
- package tlg.sence
- {
- import flash.display.DisplayObjectContainer;
- import flash.display.Sprite;
- import tlg.tool.PlayVideo;
- public class Sence extends Sprite
- {
- public var playVideo:PlayVideo ;
- public function Sence( )
- {
- }
- public function addSenceContent ( str:String ):void {
- playVideo = new PlayVideo ( );
- playVideo._stream.play( str );
- this.addChild( playVideo );
- }
- }
- }
那里不够好呢?首先我们看PlayVideo.as
public var _stream:NetStream; 方法是个公开方法,后果就是造成引用PlayVideo.as 时,需要额外调一下这个个属性:playVideo._stream.play( str );
public var vidoUrl:String; 方法是个公开方法;
_video = new Video(160, 120); 参数已经写死;为以后其他类引用造成局限。同理layVideo 没有设有必要的位置信息属性,虽然我们可以,呵呵,你懂的
同样的在以下代码中也将数据写死了,
- // 滑块条
- _track= new Sprite( );
- _track.graphics.lineStyle(1,0xcccccc );
- _track.graphics.drawRect(0, -2.5,150, 5);
- _track.graphics.endFill();
- _track.y = _video.height + 20;
- this.addChild(_track);
- // 滑块
- _thumb = new Sprite( );
- _thumb.graphics.lineStyle( );
- _thumb.graphics.beginFill(0x000000);
- _thumb.graphics.drawRect(-5, -5, 10, 10);
- _thumb.graphics.endFill( );
好吧,给出解决以上不足的一个方法,就是抽象PlayVideo 类。怎么抽象?就是挑出所有引用某个值的变量。比如:
- //
- public var _stream:NetStream;
- //
- private var _video:Video;
- //
- private var connection:NetConnection;
- public var _duration:uint;
- // 视频链接地址
- public var vidoUrl:String;
和写死的数据,如
- new Video(160, 120);
下面要做的是就是将PlayVideo 抽象出来
- public function PlayVideo( nc:NetConnection,ns: NetStream,video:Video,flick:string,xpos:uint,ypos:uint,sizeW:uint,sizeY:uint ) {
- nc = new NetConnection ();
- nc.connect ( null );
- ns = new NetStream ( nc );
- vid = new video ( sizeW,sizeY );
- vid.attachNetStream ( ns );
- ns.play ( flick );
- vid .x = xPos;
- vid.y = yPos;
- addchild ( vid );
- }
那究竟什么是抽象?为什么要抽象? 你可能通过其他途径获取了其要点。我举个榨汁机的例子:当你要榨汁时,只需把水果(参数)放进去,调整几个按钮(参数),并不关心它是怎么榨汁的。
这也就是我们常说的黑盒子封装,封装很重要,因为在具体程序中强调,多依赖组合,而不是继承。