package src {
/**
* 张启龙 2010 3 4
* ansonzql@gmail.com
* this class is for loading and unloading flv without add any player
*/
import flash.display.*;
import flash.events.*;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.display.MovieClip;
import flash.ui.Mouse;
import flash.utils.Timer;
import flash.media.SoundMixer;
public class CLoadFlv
{
private var videoURL:String;
private var loop:Boolean;
private var connection:NetConnection;
private var stream:NetStream;
public static var _h:Number = 0;
public static var _w:Number = 0;
public static var _t:Number = 0;
public var sdd:Boolean = false;
public var video:Video;
private var _time_out:Timer;
private const times_out:Number = 20;
private var _display:DisplayObjectContainer;
public function CLoadFlv(_videoURL:String,dis:DisplayObjectContainer, _loop:Boolean = true)
{
if (_videoURL == "")
{
new Error("video can't be null!");
}
else
{
this.videoURL = _videoURL;
}
if (dis != null)
{
this._display = dis;
}
this.loop = _loop;
this._time_out = new Timer(times_out, 101);
this._time_out.addEventListener(TimerEvent.TIMER, Get_out);
this.connection = new NetConnection();
this.connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
this.connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
this.connection.connect(null);
}
//淡出
private function Get_out(e:TimerEvent)
{
if (this.video.alpha > 0)
{
this.video.alpha -= 0.02;
}
else
{
this.video.alpha = 0;
this._time_out.stop();
this._time_out.reset();
this.release_flv();
}
}
//施放资源
private function release_flv():void
{
this._display.removeChild(video);
this._display.removeEventListener(Event.ENTER_FRAME,check_over);
this.video = null;
this.stream = null;
SoundMixer.stopAll();
}
//加载前的信息
private function netStatusHandler(event:NetStatusEvent):void {
switch (event.info.code) {
case "NetConnection.Connect.Success":
this.connectStream();
break;
case "NetStream.Play.StreamNotFound":
trace("Unable to locate video: " + videoURL);
break;
}
}
private function connectStream():void {
this.stream = new NetStream(connection);
this.stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
this.stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
this.stream.client = new CustomClient();
this.video = new Video();
this.video.attachNetStream(stream);
this.video.smoothing = true;
this.stream.play(videoURL);
this._display.addEventListener(Event.ENTER_FRAME,check_over)
}
public function playnext():void
{
this._time_out.start();
}
//这里要重新设置过大小
public function check_over(e:Event)
{
if(_w != 0 && this.sdd == false)
{
this.sdd = true;
this. _display.addChildAt(video,1);
this.video.width = _w;
this.video.height = _h;
}
if (this.stream.time == _t && this.loop == true )
{
this.stream.seek(0);
}
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler: " + event);
}
private function asyncErrorHandler(event:AsyncErrorEvent):void {
// ignore AsyncErrorEvent events.
}
}
}
//客户端
class CustomClient {
public function onMetaData(info:Object):void {
trace("metadata: duration=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate);
src.CLoadFlv._h = info.height;
src.CLoadFlv._w = info.width;
src.CLoadFlv._t = info.duration
}
public function onCuePoint(info:Object):void {
trace("cuepoint: time=" + info.time + " name=" + info.name + " type=" + info.type);
}
public function onXMPData(info:Object):void
{
}
}
浙公网安备 33010602011771号