Flash 外部加载flv(1)
原文地址:http://blog.csdn.net/hero82748274/category/421881.aspx?PageNumber=10
package
{
import flash.media.*;
import flash.events.*;
import flash.net.*;
import flash.display.Sprite;
import FVideo;
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 sound:SoundTransform=new SoundTransform();
public function FLVPlayer(loops:int=1,startTime:Number=0)
{
super();
playhead=startTime;
loops=loops;
init();
}
private function init():void
{
video=new FLVideo();
var connection:NetConnection=new NetConnection();
connection.connect(null);
stream=new NetStream(connection);
trace(stream.soundTransform.volume);
}
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 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);
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 0;
}
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 0;
}
//检测是不是在播放
public function get playing():Boolean
{
return true;
}
//返回加载进度
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 replay():void
{
}
//暂停
public function pause():void
{
stream.pause();
}
//关闭
public function close():void
{
stream.close();
}
// 回放
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;
}
}
}
package
{
import flash.media.*;
import flash.events.*;
import flash.net.*;
import flash.display.Sprite;
import FVideo;
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 sound:SoundTransform=new SoundTransform();
public function FLVPlayer(loops:int=1,startTime:Number=0)
{
super();
playhead=startTime;
loops=loops;
init();
}
private function init():void
{
video=new FLVideo();
var connection:NetConnection=new NetConnection();
connection.connect(null);
stream=new NetStream(connection);
trace(stream.soundTransform.volume);
}
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 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);
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 0;
}
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 0;
}
//检测是不是在播放
public function get playing():Boolean
{
return true;
}
//返回加载进度
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 replay():void
{
}
//暂停
public function pause():void
{
stream.pause();
}
//关闭
public function close():void
{
stream.close();
}
// 回放
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;
}
}
}
首先封装一个类,用于外部加载视频,由于这个类还可以继续完善的。暂时写了第一个版本,有兴趣可以增加其需求继续完善他,日后会对其进行修改。继续完善
view plaincopy to clipboardprint?
package
{
import flash.media.Video;
import flash.events.*;
public class FLVideo extends Video
{
public var url:String=null;
public function FLVideo()
{
}
}
}
package
{
import flash.media.Video;
import flash.events.*;
public class FLVideo extends Video
{
public var url:String=null;
public function FLVideo()
{
}
}
}
扩展video 增加一个连接。用于记录视频的地址。
下面则是测试源码:可以设置声音
view plaincopy to clipboardprint?
player=new FLVPlayer();
addChild(player.contain);
player.load("1.flv",true);
layer.play();
player.volume=0.1;
trace("声音"+player.volume);
player.videoWidth=550;
player.videoHeight=350;
player=new FLVPlayer();
addChild(player.contain);
player.load("1.flv",true);
player.play();
player.volume=0.1;
trace("声音"+player.volume);
player.videoWidth=550;
player.videoHeight=350;
如果希望进行播放,则加上 player.play();就可以。
设置声音大小则可以设置FLVPlayer类的属性。里面的类还有部分功能还没有实现到
1.0版本,实现了外部加载的功能
设置声音,
视频宽度和高度
进度,设置缓冲时间,
基本的视频控制等基础功能
获取视频的当前视频时间
没有实现的还有多列表播放,添加多列表播放功能,还有获取总的视频时间
后面的版本会使用air程序进行开发。希望可以进行完善
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/hero82748274/archive/2009/06/25/4298797.aspx