AS3 CookBook学习整理(十三)

1. 控制视频声音

使用NetStream对象的soundTransform属性

package {
 import flash.display.Sprite;
 import flash.events.MouseEvent;
 import flash.media.SoundTransform;
 import flash.media.Video;
 import flash.net.NetConnection;
 import flash.net.NetStream;
 public class Sample0623 extends Sprite
 {
  private var ns:NetStream;
  
  public function Sample0623()
  {
   var nc:NetConnection = new NetConnection();
   nc.connect(null);
   ns = new NetStream(nc);
   ns.client = this;
   var video:Video = new Video();
   video.attachNetStream(ns);
   ns.play("demo.flv");
   stage.addEventListener(MouseEvent.CLICK,onClick);
   this.addChild(video);
  }
  
  private function onClick(event:MouseEvent):void
  {
   var transform:SoundTransform = ns.soundTransform;
   transform.volume = .2;
   ns.soundTransform = transform;
  }
  
  public function onMetaData(infoObject:Object):void
  {
   for(var proName:String  in  infoObject)
   {
    trace(proName+":"+infoObject[proName]);
   }
  }
 }
}

2. 获得播放时间(播放头)

使用NetStream对象的time属性,单位为秒。

textField.text = netStream.time + " seconds";

注意其值没有经过四舍五入,也就是说有时候得到数可能诸如5.235,如果需要整数,可使用Math.round(), Math.floor(), 或Math.ceil()

time属性是只读的

3. 获得视频总时间

使用onMetaData()回调函数读取duration源数据值,单位为秒

NetStream类没有定义一个属性指明视频长度,不过大多数情况,可以从flv文件的元数据获得长度值

当一个NetStream对象载入一个flv文件,它会自动调用onMetaData( )回调方法,该回调模型不同于ActionScript 3.0 API 中的一般事件模型,一般情况下我们通过使用addEventListener( ). 对一个事件进行监听,但是元数据事件必须为一个对象定义一个叫onMetaData( )方法,然后把这个对象赋值给NetStream对象的client属性。该方法自动传递一个类型为Object的关联数组,其包含flv文件的元数据信息

package {
 import flash.display.Sprite;
 import flash.media.Video;
 import flash.net.NetConnection;
 import flash.net.NetStream;
 public class Sample0623 extends Sprite
 {
  private var ns:NetStream;
  
  public function Sample0623()
  {
   var nc:NetConnection = new NetConnection();
   nc.connect(null);
   ns = new NetStream(nc);
   var client:Object = new Object();
   client.onMetaData = onMetaData;
   ns.client = client;
   var video:Video = new Video();
   video.attachNetStream(ns);
   ns.play("demo.flv");
   this.addChild(video);
  }
  private function onMetaData(data:Object):void
  {
   trace(data.duration);
  }
 }
}

4. 控制播放时间(播放头位置)

使用NetStream的seek()方法

当使用seek()时,Flash播放器将搜寻最接近于指定位置(从流的开始位置算起的偏移量,以秒为单位)的关键帧。到达该位置时,流将恢复播放

视频搜寻的确切位置取决于导出视频时的帧频率 (fps) 设置。 因此,如果以 6 fps 和 30 fps 的帧频率导出同一个视频,并对两个视频对象都使用myStream.seek(15),则这两个视频将搜寻两个不同的位置

如要返回到该流的开始处,请seek(0)

package {
 import flash.display.Sprite;
 import flash.events.MouseEvent;
 import flash.media.Video;
 import flash.net.NetConnection;
 import flash.net.NetStream;
 public class Sample0623 extends Sprite
 {
  private var ns:NetStream;
  
  public function Sample0623()
  {
   var nc:NetConnection = new NetConnection();
   nc.connect(null);
   ns = new NetStream(nc);
   var client:Object = new Object();
   client.onMetaData = onMetaData;
   ns.client = client;
   var video:Video = new Video();
   video.attachNetStream(ns);
   ns.play("demo.flv");
   
   stage.addEventListener(MouseEvent.CLICK,onClick);
   this.addChild(video);
  }
  
  private function onClick(event:MouseEvent):void
  {
   ns.seek(ns.time + 15);
  }
  private function onMetaData(data:Object):void
  {
  }
 }
}

5. 改变视频的显示尺寸

设置Video对象的width和height属性

Video类定义了两个只读属性:videoWidth和videoHeight,他们返回视频编码时的尺寸,可以用这两个属性设置Video的width和height值

video.width = video.videoWidth;

video.height = video.videoHeight;

需要注意的是,flv还没下载之前这两个属性是不可用的,因此必须等两个属性可用时才行,可以监听netStatus事件达到目的:

PS: NetConnection、NetStream对象报告其状态时,将调度NetStatusEvent对象。只有一种类型的状态事件:NetStatusEvent.NET_STATUS

package {
 import flash.display.Sprite;
 import flash.events.NetStatusEvent;
 import flash.media.Video;
 import flash.net.NetConnection;
 import flash.net.NetStream;
 public class Sample0623 extends Sprite
 {
  public function Sample0623()
  {
   var nc:NetConnection = new NetConnection();
   nc.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus);
   nc.connect(null);
  }
  
  private function onNetStatus(event:NetStatusEvent):void
  {
   if(event.info.code=="NetConnection.Connect.Success")
   {
    var ns:NetStream = new NetStream(event.target  as  NetConnection);
    ns.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus);
    ns.client = this;
    ns.play("demo2.flv");
    
    var video:Video = new Video();
    video.attachNetStream(ns);
    if(video.videoWidth>0 && video.width!=video.videoWidth)
    {
     video.width = video.videoWidth;
     video.height = video.videoHeight;  
    }
    this.addChild(video);
   }
  }
  
  public function onMetaData(infoObject:Object):void
  {
  }
 }
}

6. 管理和监视缓冲及下载进度

利用NetStream的以下属性:

bufferTime -- 缓冲区大小。可设置(单位为秒),默认为0.1秒

bufferLength -- 已进入缓冲区的秒数

bufferLength / bufferTime -- 已缓冲的百分比

bytesLoaded -- 已下载的字节数

bytesTotal -- 总字节数

bytesLoaded / bytesTotal -- 已下载的百分比

package {
 import flash.display.Sprite;
 import flash.events.Event;
 import flash.events.MouseEvent;
 import flash.media.Video;
 import flash.net.NetConnection;
 import flash.net.NetStream;
 import flash.text.TextField;
 import flash.text.TextFieldAutoSize;
 public class Sample0623 extends Sprite
 {
  private var textBox:TextField;
  private var ns:NetStream;
  
  public function Sample0623()
  {
   //Video 
   var nc:NetConnection = new NetConnection();
   nc.connect(null);
   ns = new NetStream(nc);
   ns.client = this;
   ns.bufferTime = 5;
   ns.play("demo.flv");
   var video:Video = new Video();
   video.attachNetStream(ns);
   this.addChild(video);
   
   //Text 
   textBox = new TextField();
   textBox.autoSize = TextFieldAutoSize.CENTER;
   textBox.multiline = true;
   textBox.x = 100;
   textBox.y = 250;
   this.addChild(textBox);
   
   stage.addEventListener(Event.ENTER_FRAME,onEnterFrame);
  }
  
  private function onEnterFrame(event:Event):void
  {
   textBox.text = "";
   textBox.appendText("缓冲区大小是:"+ns.bufferTime+"\n");
   textBox.appendText("已进入缓冲区的秒数:"+ns.bufferLength+"\n");
   textBox.appendText("已缓冲的百分比:"+ Math.round((ns.bufferLength/ns.bufferTime)*100) +"%\n");
   textBox.appendText("已下载的字节数:"+ns.bytesLoaded+"\n");
   textBox.appendText("总字节数:"+ns.bytesTotal+"\n");
   textBox.appendText("已下载的百分比:"+ Math.round((ns.bytesLoaded/ns.bytesTotal)*100) +"%\n");
  }
  
  public function onMetaData(infoObject:Object):void
  {
  }
 }
}

7. 监听flv的提示点

使用onCuePoint( )回调函数

和onMetaData( ) 一样onCuePoint( )方法定义为Object,然后赋值给NetStream对象的client属性。onCuePoint( )方法接受一个参数,类型为object,属性有:

name -- 提示点的名称

time -- 提示点所在位置的时间

type -- 无论是"event"或"navigation," 都由编码时所选择的类型决定

parameters -- key/value 对组成的关联数组

package {
 import flash.display.Sprite;
 import flash.media.Video;
 import flash.net.NetConnection;
 import flash.net.NetStream;
 public class Sample0623 extends Sprite
 { 
  public function Sample0623()
  {
   var nc:NetConnection = new NetConnection();
   nc.connect(null);
   var ns:NetStream = new NetStream(nc);
   
   var client:Object = new Object( );
   client.onCuePoint = onCuePoint;
   ns.client = client;
   ns.play("demo2.flv");
   
   var video:Video = new Video();
   video.attachNetStream(ns);
   this.addChild(video);
  }
  
  public function onCuePoint(cuePoint:Object):void
  {
   trace(cuePoint.name + ":" + cuePoint.time);
  }
 }
}

8. 暂停和继续播放视频

使用NetStream对象的pause()和resume()方法

package {
 import flash.display.Sprite;
 import flash.events.MouseEvent;
 import flash.media.Video;
 import flash.net.NetConnection;
 import flash.net.NetStream;
 public class Sample0624 extends Sprite
 {
  private var ns:NetStream;
  private var isPause:Boolean = false;
  
  public function Sample0624()
  {
   var nc:NetConnection = new NetConnection();
   nc.connect(null);
   
   ns = new NetStream(nc);
   var client:Object = new Object();
   client.onMetaData = onMetaData;
   ns.client = client;
   ns.play("demo.flv");
   
   var video:Video = new Video;
   video.attachNetStream(ns);
   this.addChild(video);
  
   stage.addEventListener(MouseEvent.CLICK,onClick);
  }
  
  private function onClick(event:MouseEvent):void
  {
   if(isPause)
   {
    ns.resume();
    isPause = false;
   }
   else
   {
    ns.pause();
    isPause = true;
   }
  }
  
  private function onMetaData(infoObject:Object):void
  {
   trace(infoObject.duration);
  }
 }
}

9. 停止播放视频

使用NetStream类的close()方法

pause()方法只是暂停播放,flv数据还是在继续下载,如果要完全停止视频下载,必须使用close()方法

当调用close()方法后,Flash Player删除内存中的flv数据,要想播放需要重新下载一遍。但是浏览器可能已经缓存了flv数据,因此第二次播放可能会快一些,但是这可能导致不适最新的数据,如果想每次都是播放最新的数据,可以再url上加上唯一字符串,如下面的代码:

netStream.play("video.flv?uniqueIndex=" + (new Date()).getTime());

如果因为数字版权问题不想视频数据被缓存,就不能使用渐进式下载视频,这种情况可使用流播放,例如可使用Flash Media Server

10. 快进或快退播放(滑块控制)

package {
 import flash.display.Sprite;
 import flash.events.Event;
 import flash.events.MouseEvent;
 import flash.geom.Rectangle;
 import flash.media.Video;
 import flash.net.NetConnection;
 import flash.net.NetStream;
 
 public class Sample0624 extends Sprite
 {
  private var ns:NetStream;
  private var timeLine:Sprite;
  private var bar:Sprite;
  private var videoTime:Number;
  private var barIsUp:Boolean = true;
  private var barIsMove:Boolean = false;
  
  public function Sample0624()
  {
   //Video 
   var nc:NetConnection = new NetConnection();
   nc.connect(null);
   ns = new NetStream(nc);
   var client:Object = new Object();
   client.onMetaData = onMetaData;
   ns.client = client;
   ns.play("demo.flv");
   var video:Video = new Video();
   video.attachNetStream(ns);
   this.addChild(video);
   
   timeLine = new Sprite;
   timeLine.graphics.beginFill(0x000000);
   timeLine.graphics.drawRect(0,0,200,3);
   timeLine.graphics.endFill();
   timeLine.x = 50;
   timeLine.y = 250;
   this.addChild(timeLine);
   
   bar = new Sprite();
   bar.graphics.beginFill(0xFFFF00);
   bar.graphics.drawRect(0,0,8,15);
   bar.graphics.endFill();
   bar.y = -6;
   bar.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
   bar.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
   timeLine.addChild(bar);
   
   stage.addEventListener(Event.ENTER_FRAME,onEnterFrame);
  }
  
  private function onEnterFrame(event:Event):void
  {
   if(barIsUp)
   {
    var percent:Number = bar.x / 200;
    ns.seek(bar.x / timeLine.width * videoTime);
    barIsUp = false;
   }
   else 
   {
    if(!barIsMove)
    {
     bar.x = ns.time / videoTime * 200;
    }
   }
  }
  
  private function onMouseDown(event:MouseEvent):void
  {
   var rectangle:Rectangle = new Rectangle(0,-6,timeLine.width-8,0);
   bar.startDrag(false,rectangle);
   barIsMove = true;
  }
  
  private function onMouseUp(event:MouseEvent):void
  {
   bar.stopDrag();
   barIsUp = true;
   barIsMove = false;
  }
  
  private function onMetaData(infoObject:Object):void
  {
   videoTime = infoObject.duration;
  }
 }
}

 

你可能感兴趣的:(as3)