AS3 CookBook学习整理(十三)

本文详细介绍如何使用Flash NetStream对象控制视频播放,包括调整音量、获取播放时间和视频总时长、控制播放进度、改变视频尺寸、监控缓冲进度、监听提示点、暂停与继续播放、停止播放、快进快退等功能。

1. 控制视频声音

使用NetStream对象的soundTransform属性

 {
 flash.display.Sprite;
 flash.events.MouseEvent;
 flash.media.SoundTransform;
 flash.media.Video;
 flash.net.NetConnection;
 flash.net.NetStream;
  Sample0623Sprite
 {
  ns:NetStream;
  
   Sample0623()
  {
   nc:NetConnection = NetConnection();
   nc.connect(null);
   ns = NetStream(nc);
   ns.client = this;
   video:Video = Video();
   video.attachNetStream(ns);
   ns.play();
   stage.addEventListener(MouseEvent.CLICK,onClick);
   .addChild(video);
  }
  
   onClick(event:MouseEvent):
  {
   transform:SoundTransform = ns.soundTransform;
   transform.volume = .2;
   ns.soundTransform = transform;
  }
  
   onMetaData(infoObject:Object):
  {
   (proName:String    infoObject)
   {
    (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文件的元数据信息

 {
 flash.display.Sprite;
 flash.media.Video;
 flash.net.NetConnection;
 flash.net.NetStream;
  Sample0623Sprite
 {
  ns:NetStream;
  
   Sample0623()
  {
   nc:NetConnection = NetConnection();
   nc.connect(null);
   ns = NetStream(nc);
   client:Object = Object();
   client.onMetaData = onMetaData;
   ns.client = client;
   video:Video = Video();
   video.attachNetStream(ns);
   ns.play();
   .addChild(video);
  }
   onMetaData(data:Object):
  {
   (data.duration);
  }
 }
}

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

使用NetStream的seek()方法

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

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

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

 {
 flash.display.Sprite;
 flash.events.MouseEvent;
 flash.media.Video;
 flash.net.NetConnection;
 flash.net.NetStream;
  Sample0623Sprite
 {
  ns:NetStream;
  
   Sample0623()
  {
   nc:NetConnection = NetConnection();
   nc.connect(null);
   ns = NetStream(nc);
   client:Object = Object();
   client.onMetaData = onMetaData;
   ns.client = client;
   video:Video = Video();
   video.attachNetStream(ns);
   ns.play();
   
   stage.addEventListener(MouseEvent.CLICK,onClick);
   .addChild(video);
  }
  
   onClick(event:MouseEvent):
  {
   ns.seek(ns.time + 15);
  }
   onMetaData(data:Object):
  {
  }
 }
}

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

 {
 flash.display.Sprite;
 flash.events.NetStatusEvent;
 flash.media.Video;
 flash.net.NetConnection;
 flash.net.NetStream;
  Sample0623Sprite
 {
   Sample0623()
  {
   nc:NetConnection = NetConnection();
   nc.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus);
   nc.connect(null);
  }
  
   onNetStatus(event:NetStatusEvent):
  {
   (event.info.code==)
   {
    ns:NetStream = NetStream(event.target    NetConnection);
    ns.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus);
    ns.client = this;
    ns.play();
    
    video:Video = Video();
    video.attachNetStream(ns);
    (video.videoWidth>0 && video.width!=video.videoWidth)
    {
     video.width = video.videoWidth;
     video.height = video.videoHeight;  
    }
    .addChild(video);
   }
  }
  
   onMetaData(infoObject:Object):
  {
  }
 }
}

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

利用NetStream的以下属性:

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

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

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

bytesLoaded -- 已下载的字节数

bytesTotal -- 总字节数

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

 {
 flash.display.Sprite;
 flash.events.Event;
 flash.events.MouseEvent;
 flash.media.Video;
 flash.net.NetConnection;
 flash.net.NetStream;
 flash.text.TextField;
 flash.text.TextFieldAutoSize;
  Sample0623Sprite
 {
  textBox:TextField;
  ns:NetStream;
  
   Sample0623()
  {
    
   nc:NetConnection = NetConnection();
   nc.connect(null);
   ns = NetStream(nc);
   ns.client = this;
   ns.bufferTime = 5;
   ns.play();
   video:Video = Video();
   video.attachNetStream(ns);
   .addChild(video);
   
    
   textBox = TextField();
   textBox.autoSize = TextFieldAutoSize.CENTER;
   textBox.multiline = ;
   textBox.x = 100;
   textBox.y = 250;
   .addChild(textBox);
   
   stage.addEventListener(Event.ENTER_FRAME,onEnterFrame);
  }
  
   onEnterFrame(event:Event):
  {
   textBox.text = ;
   textBox.appendText(+ns.bufferTime+);
   textBox.appendText(+ns.bufferLength+);
   textBox.appendText(+ Math.round((ns.bufferLength/ns.bufferTime)*100) +);
   textBox.appendText(+ns.bytesLoaded+);
   textBox.appendText(+ns.bytesTotal+);
   textBox.appendText(+ Math.round((ns.bytesLoaded/ns.bytesTotal)*100) +);
  }
  
   onMetaData(infoObject:Object):
  {
  }
 }
}

7. 监听flv的提示点

使用onCuePoint( )回调函数

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

name -- 提示点的名称

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

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

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

 {
 flash.display.Sprite;
 flash.media.Video;
 flash.net.NetConnection;
 flash.net.NetStream;
  Sample0623Sprite
 { 
   Sample0623()
  {
   nc:NetConnection = NetConnection();
   nc.connect(null);
   ns:NetStream = NetStream(nc);
   
   client:Object = Object( );
   client.onCuePoint = onCuePoint;
   ns.client = client;
   ns.play();
   
   video:Video = Video();
   video.attachNetStream(ns);
   .addChild(video);
  }
  
   onCuePoint(cuePoint:Object):
  {
   (cuePoint.name +  + cuePoint.time);
  }
 }
}

8. 暂停和继续播放视频

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

 {
 flash.display.Sprite;
 flash.events.MouseEvent;
 flash.media.Video;
 flash.net.NetConnection;
 flash.net.NetStream;
  Sample0624Sprite
 {
  ns:NetStream;
  isPause:Boolean = ;
  
   Sample0624()
  {
   nc:NetConnection = NetConnection();
   nc.connect(null);
   
   ns = NetStream(nc);
   client:Object = Object();
   client.onMetaData = onMetaData;
   ns.client = client;
   ns.play();
   
   video:Video = Video;
   video.attachNetStream(ns);
   .addChild(video);
  
   stage.addEventListener(MouseEvent.CLICK,onClick);
  }
  
   onClick(event:MouseEvent):
  {
   (isPause)
   {
    ns.resume();
    isPause = ;
   }
   else
   {
    ns.pause();
    isPause = ;
   }
  }
  
   onMetaData(infoObject:Object):
  {
   (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. 快进或快退播放(滑块控制)

 {
 flash.display.Sprite;
 flash.events.Event;
 flash.events.MouseEvent;
 flash.geom.Rectangle;
 flash.media.Video;
 flash.net.NetConnection;
 flash.net.NetStream;
 
  Sample0624Sprite
 {
  ns:NetStream;
  timeLine:Sprite;
  bar:Sprite;
  videoTime:Number;
  barIsUp:Boolean = ;
  barIsMove:Boolean = ;
  
   Sample0624()
  {
    
   nc:NetConnection = NetConnection();
   nc.connect(null);
   ns = NetStream(nc);
   client:Object = Object();
   client.onMetaData = onMetaData;
   ns.client = client;
   ns.play();
   video:Video = Video();
   video.attachNetStream(ns);
   .addChild(video);
   
   timeLine = Sprite;
   timeLine.graphics.beginFill(0x000000);
   timeLine.graphics.drawRect(0,0,200,3);
   timeLine.graphics.endFill();
   timeLine.x = 50;
   timeLine.y = 250;
   .addChild(timeLine);
   
   bar = 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);
  }
  
   onEnterFrame(event:Event):
  {
   (barIsUp)
   {
    percent:Number = bar.x / 200;
    ns.seek(bar.x / timeLine.width * videoTime);
    barIsUp = ;
   }
   else 
   {
    (!barIsMove)
    {
     bar.x = ns.time / videoTime * 200;
    }
   }
  }
  
   onMouseDown(event:MouseEvent):
  {
   rectangle:Rectangle = Rectangle(0,-6,timeLine.width-8,0);
   bar.startDrag(false,rectangle);
   barIsMove = ;
  }
  
   onMouseUp(event:MouseEvent):
  {
   bar.stopDrag();
   barIsUp = ;
   barIsMove = ;
  }
  
   onMetaData(infoObject:Object):
  {
   videoTime = infoObject.duration;
  }
 }
}

 

转载于:https://www.cnblogs.com/CoderWayne/archive/2010/07/15/1778077.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值