Flash 外部加载flv(2)
package
{
import flash.media.*;
import flash.events.*;
import flash.net.*;
import flash.display.Sprite;
import flash.display.Stage;
import flash.display.StageDisplayState;
import FVideo;
import flash.geom.Rectangle;
public class FLVPlayer extends EventDispatcher
{
private var video:FLVideo;
private var playhead:Number=0;
private var loaded:Boolean=false;
private var loops:int;
private var stream:NetStream;
private var autoplay:Boolean=false;
private var videoURL:String=null;
public var contain:Sprite=new Sprite();
private var duration:Number=0;//计算总的时间长度
private var sound:SoundTransform=new SoundTransform();
public function FLVPlayer()
{
super();
init();
}
private function init():void
{
video=new FLVideo();
var connection:NetConnection=new NetConnection();
connection.connect(null);
stream=new NetStream(connection);
var client:Object=new Object();
client.onMetaData=onMetaData;//回调函数,获取总的时间
stream.client=client;
}
public function load(url:String=null,autoplay:Boolean=false,bufferSeconds:Number=1):void
{
if (video.url == null && percentLoaded == 0)
{
video.url=url;
autoplay=autoplay;
video.attachNetStream(stream);
contain.addChild(video);//添加容器
stream.bufferTime=bufferSeconds;//设置缓冲时间
stream.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,netAsyncErrorHandler);
}
}
//添加多视频,进行管理
//public function add(url:String):void
//{
//
//}
private function onMetaData(data:Object):void
{
duration=data.duration;
}
private function netStatusHandler(event:NetStatusEvent):void
{
switch (event.info.code)
{
case "NetConnection.Connect.Success" :
break;
case "NetStream.Play.StreamNotFound" :
throw new Error("错误");
break;
case "NetStream.Play.Stop" :
stream.seek(0);
//close();
break;
}
}
private function netAsyncErrorHandler(event:AsyncErrorEvent):void
{
}
/*
@ buffertime设置缓冲时间
@ bufferLength 数据当前存在于缓冲区中的秒数。
@
*/
public function get bufferLength():Number
{
return stream.bufferLength;
}
public function set bufferTime(time:Number):void
{
stream.bufferTime=time;
}
public function get bufferTime():Number
{
return stream.bufferTime;
}
public function get FVideo():Video
{
return video;
}
//设置和返回视频播放位置(以时间为单位)
public function get position():Number
{
return stream.time;
}
public function set position(time:Number):void
{
stream.seek(time);
}
/*
@设置和返回视频的声音大小
*/
public function get volume():Number
{
var volumes:Number=sound.volume;
stream.soundTransform=sound;
return volumes;
}
public function set volume(volumes:Number):void
{
sound.volume=volumes;
stream.soundTransform=sound;
}
//获取返回视频的总的时间
public function get totalTime():Number
{
return duration;
}
//检测是不是在播放
public function get playing():Boolean
{
return true;
}
//加载进度
public function get bytesLoaded():uint
{
return stream.bytesLoaded;
}
//加载总量
public function get bytesTotal():uint
{
return stream.bytesTotal;
}
//返回加载进度
public function get percentLoaded():Number
{
var percent:Number=stream.bytesLoaded / stream.bytesTotal;
return isNaN(percent)?0:percent;
}
/*
@ 下面是视频播放相关控制的接口
@
@
@
*/
//播放视频
public function play():void
{
if (video.url != null)
{
stream.play(video.url);
}
}
//暂停
public function pause():void
{
stream.pause();
}
//关闭
public function close():void
{
stream.close();
}
//停止播放
public function stop():void
{
stream.close();
stream.seek(0);
}
// 回放
public function togglePause():void
{
stream.togglePause();
}
//清除视频
public function clear():void
{
video.clear();
}
//重新播放
public function resume():void
{
stream.resume();
}
//设置视频是否平滑
public function set smoothing(flag:Boolean):void
{
video.smoothing=flag;
}
//设置视频高度和宽度
public function get videoHeight():int
{
return video.videoHeight;
}
public function set videoHeight(height:int):void
{
video.height=height;
}
public function get videoWidth():int
{
return video.videoWidth;
}
public function set videoWidth(width:int):void
{
video.width=width;
}
//全屏模式
public function fullScreen(stage:Stage):void
{
if (stage.displayState==StageDisplayState.NORMAL)
{
//var screenRectangle:Rectangle = new Rectangle(video.x, video.y, video.width, video.height);
// stage.fullScreenSourceRect = screenRectangle;
stage.displayState = StageDisplayState.FULL_SCREEN;
}
else if (stage.displayState ==StageDisplayState.FULL_SCREEN)
{
stage.displayState=StageDisplayState.NORMAL;
}
}
}
}
package
{
import flash.media.*;
import flash.events.*;
import flash.net.*;
import flash.display.Sprite;
import flash.display.Stage;
import flash.display.StageDisplayState;
import FVideo;
import flash.geom.Rectangle;
public class FLVPlayer extends EventDispatcher
{
private var video:FLVideo;
private var playhead:Number=0;
private var loaded:Boolean=false;
private var loops:int;
private var stream:NetStream;
private var autoplay:Boolean=false;
private var videoURL:String=null;
public var contain:Sprite=new Sprite();
private var duration:Number=0;//计算总的时间长度
private var sound:SoundTransform=new SoundTransform();
public function FLVPlayer()
{
super();
init();
}
private function init():void
{
video=new FLVideo();
var connection:NetConnection=new NetConnection();
connection.connect(null);
stream=new NetStream(connection);
var client:Object=new Object();
client.onMetaData=onMetaData;//回调函数,获取总的时间
stream.client=client;
}
public function load(url:String=null,autoplay:Boolean=false,bufferSeconds:Number=1):void
{
if (video.url == null && percentLoaded == 0)
{
video.url=url;
autoplay=autoplay;
video.attachNetStream(stream);
contain.addChild(video);//添加容器
stream.bufferTime=bufferSeconds;//设置缓冲时间
stream.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,netAsyncErrorHandler);
}
}
//添加多视频,进行管理
//public function add(url:String):void
//{
//
//}
private function onMetaData(data:Object):void
{
duration=data.duration;
}
private function netStatusHandler(event:NetStatusEvent):void
{
switch (event.info.code)
{
case "NetConnection.Connect.Success" :
break;
case "NetStream.Play.StreamNotFound" :
throw new Error("错误");
break;
case "NetStream.Play.Stop" :
stream.seek(0);
//close();
break;
}
}
private function netAsyncErrorHandler(event:AsyncErrorEvent):void
{
}
/*
@ buffertime设置缓冲时间
@ bufferLength 数据当前存在于缓冲区中的秒数。
@
*/
public function get bufferLength():Number
{
return stream.bufferLength;
}
public function set bufferTime(time:Number):void
{
stream.bufferTime=time;
}
public function get bufferTime():Number
{
return stream.bufferTime;
}
public function get FVideo():Video
{
return video;
}
//设置和返回视频播放位置(以时间为单位)
public function get position():Number
{
return stream.time;
}
public function set position(time:Number):void
{
stream.seek(time);
}
/*
@设置和返回视频的声音大小
*/
public function get volume():Number
{
var volumes:Number=sound.volume;
stream.soundTransform=sound;
return volumes;
}
public function set volume(volumes:Number):void
{
sound.volume=volumes;
stream.soundTransform=sound;
}
//获取返回视频的总的时间
public function get totalTime():Number
{
return duration;
}
//检测是不是在播放
public function get playing():Boolean
{
return true;
}
//加载进度
public function get bytesLoaded():uint
{
return stream.bytesLoaded;
}
//加载总量
public function get bytesTotal():uint
{
return stream.bytesTotal;
}
//返回加载进度
public function get percentLoaded():Number
{
var percent:Number=stream.bytesLoaded / stream.bytesTotal;
return isNaN(percent)?0:percent;
}
/*
@ 下面是视频播放相关控制的接口
@
@
@
*/
//播放视频
public function play():void
{
if (video.url != null)
{
stream.play(video.url);
}
}
//暂停
public function pause():void
{
stream.pause();
}
//关闭
public function close():void
{
stream.close();
}
//停止播放
public function stop():void
{
stream.close();
stream.seek(0);
}
// 回放
public function togglePause():void
{
stream.togglePause();
}
//清除视频
public function clear():void
{
video.clear();
}
//重新播放
public function resume():void
{
stream.resume();
}
//设置视频是否平滑
public function set smoothing(flag:Boolean):void
{
video.smoothing=flag;
}
//设置视频高度和宽度
public function get videoHeight():int
{
return video.videoHeight;
}
public function set videoHeight(height:int):void
{
video.height=height;
}
public function get videoWidth():int
{
return video.videoWidth;
}
public function set videoWidth(width:int):void
{
video.width=width;
}
//全屏模式
public function fullScreen(stage:Stage):void
{
if (stage.displayState==StageDisplayState.NORMAL)
{
//var screenRectangle:Rectangle = new Rectangle(video.x, video.y, video.width, video.height);
// stage.fullScreenSourceRect = screenRectangle;
stage.displayState = StageDisplayState.FULL_SCREEN;
}
else if (stage.displayState ==StageDisplayState.FULL_SCREEN)
{
stage.displayState=StageDisplayState.NORMAL;
}
}
}
}
在原来基础上新增了一个全屏模式,获取到视频时间总的时间。
使用一个时间格式进行转换对应的时间格式
这个类可以下载下来进行改造。基本的功能还有待提高。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/hero82748274/archive/2009/06/30/4307461.aspx