[AS3]as3写的FLV播放器源代码完整实例
一个简单的FLV播放器,应用了NetConnection类,NetStream类以及Video类,在创建视频之前在NetStream类对象上注册了onMetaData事件和NET_STATUS事件import flash.media.*;
import flash.net.URLStream;
import flash.net.NetConnection;
var movieTotalTime:Number=0;//CuPlayer.com提示:总时间
var proTime:Number=0;//已经播放的时间
var dx=drag.x;
var lx=line.x;
var dw=drag.width;
var lw=line.width;
var tempProgress;
var now=true;
btn.buttonMode=true;
line.line_b.scaleX=0;//CuPlayer.com提示:进度条控制
var videoconnection:NetConnection=new NetConnection();//创建一个NetConnection对象.
videoconnection.connect(null);
var videostream:NetStream=new NetStream(videoconnection);//建立一个NetStream对象,连接到NetConnection里面去.
var myvideo:Video =new Video();
myvideo.attachNetStream(videostream);//Video挂接NetStream,从NetStream那里读内容.
videostream.play("http://www.okxs.net/FLV/1.flv");
var client:Object=new Object();
client.onMetaData=onMetaData;//回调函数,获取总的时间
videostream.client=client;
function onMetaData(data:Object):void {
movieTotalTime=Math.round(data.duration);
}
//视频播放前缓冲到一定时间后才自动播放,
videostream.bufferTime=5;
var buffer:Boolean=true;
function onStatus(_evt:NetStatusEvent ):void {
trace(_evt.info.code);
if (_evt.info.code=="NetStream.Play.Start") {
addEventListener(Event.ENTER_FRAME ,onEnterFrame);
} else if (_evt.info.code=="NetStream.Buffer.Empty") {
loadmc.visible = true;
buffer_txt.visible=true;
buffer=true;//这里表示视频正在缓冲
} else if (_evt.info.code=="NetStream.Buffer.Full") {
buffer=false;
loadmc.visible = false;
buffer_txt.visible=false;
}//CuPlayer.com提示:这里表示视频缓冲区填满了
}
videostream.addEventListener(NetStatusEvent.NET_STATUS,onStatus);
function onEnterFrame(_evt:Event ) {
//这里就是显示当前缓冲了多少了
bufferbuffer_txt.htmlText=buffer?"正在缓冲:"+Math.floor(videostream.bufferLength/videostream.bufferTime*100)+"%":"";
}
//CuPlayer.com提示:视频大小和位置的控制
myvideo.x=-337;
myvideo.y=-396;
myvideo.width=483;
myvideo.height=372;
addChild(myvideo);
setChildIndex(myvideo,0);
btn_mc.visible=false;
btn_an.addEventListener(MouseEvent.MOUSE_DOWN,bt_down);
btn.addEventListener(MouseEvent.CLICK,bt_down);
line.addEventListener(MouseEvent.MOUSE_DOWN,dragdown);
line.addEventListener(MouseEvent.MOUSE_UP,dragup);
function dragdown(e:MouseEvent) {
drag.x=mouseX;
proTime=(drag.x-dx)/line.width*movieTotalTime;//当前播放的时间
videostream.seek(proTime);
}
function dragup(e:MouseEvent) {
removeEventListener(MouseEvent.MOUSE_DOWN,dragdown);
}
addEventListener(Event.ENTER_FRAME,dragmove);
function dragmove(e:Event) {
if (movieTotalTime!=0) {
tempProgress=Math.ceil(videostream.time/movieTotalTime*100)/100;
}
line.line_b.scaleX=tempProgress;
drag.x=Math.round(tempProgress*line.width)+dx-dw;
var ld_minutes=Math.ceil(videostream.time);
var n_minutes=Math.floor(ld_minutes/60);
var p_minutes=Math.floor(ld_minutes%60);
//CuPlayer.com提示:秒数转化为时间显示
if (p_minutes<10) {
if (n_minutes<1) {
proTitle.text="00"+":"+"0"+String(ld_minutes);
} else {
proTitle.text="0"+String(n_minutes)+":"+"0"+String(p_minutes);
}
} else {
proTitle.text="0"+String(n_minutes)+":"+String(p_minutes);
}
var str:String;
var my_minutes=Math.floor(movieTotalTime/60);//得到分钟的正整数
var my_seconds=Math.floor(movieTotalTime%60);//得到秒钟的正整数
if (my_seconds<10) {
str="0"+String(my_seconds);
} else {
str=String(my_seconds);
}
troTitle.text=String(100+my_minutes).substr(1,2)+":"+str;
}
function bt_down(e:MouseEvent):void {
now!=now;
if (now) {
videostream.pause();
btn_mc.visible=true;
btn.gotoAndStop(2);
now=false;
} else {
btn_mc.visible=false;
videostream.resume();
btn.gotoAndStop(1);
now=true;
}
if (tempProgress>=1) {
drag.x=dx;
btn_mc.visible=false;
videostream.seek(0);
videostream.resume();
}
}
//CuPlayer.com提示:全屏缩放控制
btn_screen.buttonMode=true;
btn_screen.addEventListener(MouseEvent.MOUSE_DOWN,fullScreen);
function fullScreen(e:MouseEvent):void {
if (stage.displayState==StageDisplayState.NORMAL) {
stage.displayState=StageDisplayState.FULL_SCREEN;
} else if (stage.displayState ==StageDisplayState.FULL_SCREEN) {
stage.displayState=StageDisplayState.NORMAL;
}
}
//CuPlayer.com提示:声音控制
var sound:SoundTransform =new SoundTransform();
var volumes:Number=sound.volume;
videostream.soundTransform=sound;
btn_sy.gotoAndStop(7);
for (var j:uint=1; j<=7; j++) {
var s_btn:MovieClip=btn_sy.an.getChildAt(j-1) as MovieClip;
s_btn.addEventListener(MouseEvent.MOUSE_DOWN,set_volume);
}
function set_volume(e:MouseEvent):void {
var _mc=e.currentTarget;
var k:Number=btn_sy.an.getChildIndex(_mc);
btn_sy.gotoAndStop(k+1);
sound.volume=(k+1)/7;
videostream.soundTransform=sound;
}
页:
[1]