package
{
/*
*作 者:淘剑龙
*名 称:flvplayer
*时 间:2011.1.9
*说 明:参照帮助文档写的用于学习交流的flv播放器!
*/
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.display.Loader;
import flash.media.Video;
import flash.net.URLRequest;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.*;
import flash.media.SoundTransform;
import com.greensock.*;
import com.greensock.easing.*;
import com.Data.TimeFormat;
public class flvPlayer extends Sprite
{
private var sw:Number = stage.stageWidth;
private var sh:Number = stage.stageHeight;
private var myVideo:Video;
private var myLoaderoader;
private var connection:NetConnection;
private var stream:NetStream;
private var soundControl:SoundTransform;
private var soundBol:Boolean = true; //控制声音开关
private var stopBtn:Stop; //停止按钮
private var playBtnlay; //播放按钮
private var soundBtn:SoundBtn; //声音按钮
private var pauseBtnause; //暂停按钮
private var proBar:progressBar; //进度条
private var sBtn:ScreenBtn; //舞台按钮
private var UIContainer:MovieClip;
private var videoURL:String = "flv/video.flv"; //播放的视频地址
private var playState:Boolean = true;
private var curTime:Number; //播放当前时间
private var obj:Object = new Object; //监听事件
private var per:Number = 0; //播放进度
private var per2:Number = 0; //流加载进度
private var over:Boolean = false;
private var out:Boolean = true;
private var tf:TimeFormat = new TimeFormat;
public function flvPlayer()
{
soundControl = new SoundTransform(); //控制声音
sBtn = new ScreenBtn;
UIContainer = new MovieClip;
myLoader = new Loader;
myVideo = new Video(sw,sh);
addChild(myVideo);
UIContainer.x = 50;
UIContainer.y = sh - UIContainer.height + 30;
sBtn.useHandCursor = false; //舞台按钮
sBtn.addEventListener(MouseEvent.MOUSE_OVER,OverScreen);
addChild(sBtn);
addChild(UIContainer);
init();
listener();
addEventListener(Event.ENTER_FRAME,onEnter);
}
private function init()
{
setPlayerUI();
connection = new NetConnection;
connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
connection.connect(null);
}
private function listener()
{
obj.onMetaData = function(info:Object)
{
//在 Flash Player 接收在正播放的 FLV 文件中嵌入的描述性信息时调度。
//trace("metadata: duration=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate);
obj.duration = info.duration; //视频时间长度
}
obj.onPlayStatus = function(info:Object)
{
trace("播放完毕");
}
obj.onCuePoint = function(info:Object):void
{
trace("cuepoint: time=" + info.time + " name=" + info.name + " type=" + info.type);
}
}
private function onEnter(e:Event)
{
if(over)
{
TweenLite.to(UIContainer, 0.6, {y:sh - UIContainer.height ,alpha:1});
}
else
{
TweenLite.to(UIContainer, 0.6 ,{y:sh - UIContainer.height + 30,alpha:0});
}
}
private function OverScreen(e:MouseEvent)
{
over = true;
sBtn.addEventListener(MouseEvent.MOUSE_OUT,OutScreen);
sBtn.removeEventListener(MouseEvent.MOUSE_OVER,OverScreen);
}
private function OutScreen(e:MouseEvent)
{
over = false;
TweenLite.from(UIContainer, 0.6 , {y:sh - UIContainer.height,alpha:1});
sBtn.removeEventListener(MouseEvent.MOUSE_OUT,OutScreen);
sBtn.addEventListener(MouseEvent.MOUSE_OVER,OverScreen);
}
private function setPlayerUI()
{
playBtn = new Play;
playBtn.name = "playBtn";
pauseBtn= new Pause;
pauseBtn.name= "pauseBtn";
stopBtn = new Stop;
stopBtn.name = "stopBtn";
soundBtn= new SoundBtn;
soundBtn.name= "soundBtn";
/*
*预留个全屏的
fullScreenBtn
*/
proBar = new progressBar;
proBar.name = "proBar";
var xjg:Number = 5;
pauseBtn.x = playBtn.x + (playBtn.width + xjg);
stopBtn.x = pauseBtn.x + (stopBtn.width + xjg);
soundBtn.x = stopBtn.x + (soundBtn.width+ xjg);
proBar.x = playBtn.x - playBtn.width/2;
proBar.y = pauseBtn.height - proBar.height + 5;
playBtn.btnSensor.addEventListener(MouseEvent.MOUSE_OVER,OverHandler);
playBtn.btnSensor.addEventListener(MouseEvent.MOUSE_OUT,OutHandler);
pauseBtn.btnSensor.addEventListener(MouseEvent.MOUSE_OVER,OverHandler);
pauseBtn.btnSensor.addEventListener(MouseEvent.MOUSE_OUT,OutHandler);
stopBtn.btnSensor.addEventListener(MouseEvent.MOUSE_OVER,OverHandler);
stopBtn.btnSensor.addEventListener(MouseEvent.MOUSE_OUT,OutHandler);
soundBtn.btnSensor.addEventListener(MouseEvent.MOUSE_OVER,OverHandler);
soundBtn.btnSensor.addEventListener(MouseEvent.MOUSE_OUT,OutHandler);
UIContainer.addChild(proBar);
UIContainer.addChild(stopBtn);
UIContainer.addChild(playBtn);
UIContainer.addChild(soundBtn);
UIContainer.addChild(pauseBtn);
}
private function OverHandler(e:MouseEvent)
{
over = true;
UIContainer.y = sh - UIContainer.height;
UIContainer.alpha = 1;
sBtn.removeEventListener(MouseEvent.MOUSE_OVER,OverScreen);
var str:String = e.target.parent.name;
switch (str)
{
case "playBtn" :
e.target.parent.mcIcon.mcIconAnimation.gotoAndPlay("Over");
break;
case "stopBtn" :
e.target.parent.mcIcon.mcIconAnimation.gotoAndPlay("Over");
break;
case "pauseBtn" :
e.target.parent.mcIcon.mcIconAnimation.gotoAndPlay("Over");
break;
//静音
case "soundBtn" :
e.target.parent.mcIcon.mcIconAnimation.gotoAndPlay("Over");
break;
}
}
private function OutHandler(e:MouseEvent)
{
over = true;
UIContainer.y = sh - UIContainer.height;
UIContainer.alpha = 1;
sBtn.addEventListener(MouseEvent.MOUSE_OVER,OverScreen);
var str:String = e.target.parent.name;
switch (str)
{
case "playBtn" :
e.target.parent.mcIcon.mcIconAnimation.gotoAndPlay("Out");
break;
case "stopBtn" :
e.target.parent.mcIcon.mcIconAnimation.gotoAndPlay("Out");
break;
case "pauseBtn" :
e.target.parent.mcIcon.mcIconAnimation.gotoAndPlay("Out");
break;
case "soundBtn" :
e.target.parent.mcIcon.mcIconAnimation.gotoAndPlay("Out");
break;
}
}
private function netStatusHandler(event:NetStatusEvent)
{
switch (event.info.code)
{
case "NetConnection.Connect.Success" :
connectStream();
break;
case "NetStream.Play.StreamNotFound" :
trace("Unable to locate video: " + videoURL);
break;
}
}
private function connectStream()
{
stream= new NetStream(connection);
stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
//videoURL = "flv/langjiu.flv";
myVideo.attachNetStream(stream);
stream.play(videoURL);
//调度一些事件,如获取视频时间总长度(记得视频得处理成含有onMediaData能读取视频数据的格式)
stream.client = obj; //new CustomClient;
playBtn.btnSensor.addEventListener(MouseEvent.CLICK,ClickHandler);
pauseBtn.btnSensor.addEventListener(MouseEvent.CLICK,ClickHandler);
stopBtn.btnSensor.addEventListener(MouseEvent.CLICK,ClickHandler);
soundBtn.btnSensor.addEventListener(MouseEvent.CLICK,ClickHandler);
addEventListener(Event.ENTER_FRAME,enterHandler);
}
private function enterHandler(e:Event)
{
per = stream.time / obj.duration * 100;
per2= stream.bytesLoaded / stream.bytesTotal * 100;
proBar.playPro.gotoAndStop(Math.round(per));
proBar.streamPro.gotoAndStop(Math.round(per2));
proBar.disTime.text = getTime(stream);
}
private function getTime(stream:NetStream):String
{
var str:String = new String;
var p:Number = Math.round(stream.time); //播放时间
var a:Number = Math.round(obj.duration); //视频总时间长
/*var ps:String = tf.GetTimeFormat(p);
var alls:String = tf.GetTimeFormat(a); */
str = tf.GetTimeFormat(p) + "/" + tf.GetTimeFormat(a);
return str;
}
private function asyncErrorHandler(event:AsyncErrorEvent):void
{
// ignore AsyncErrorEvent events.
}
private function ClickHandler(e:MouseEvent)
{
//点击按钮事件
var str:String = e.target.parent.name;
switch (str)
{
case "playBtn" :
//playState = !playState;
curTime = stream.time;
stream.seek(curTime);
stream.resume(); //恢复回放暂停的视频流。
//stream.togglePause(); //暂停或恢复流的回放。
break;
case "pauseBtn" :
stream.pause();//暂停
break;
case "stopBtn" :
stream.seek(0);
stream.pause();//暂停
break;
//静音
case "soundBtn" :
soundBol = !soundBol;
soundControl.volume = soundBol == true ? 1:0;
stream.soundTransform = soundControl;
/*stream.close();
stream.play(videoURL);*/
break;
}
}
}
}
这个是我根据FLASH CS3帮助文档写的个简单播放器,供大家学习交流用
里面flv文件夹里没有视频请自己添加一个进去,另外还有个flv混淆器,有的flv格式文件没有包括一些信息如视频总时间长度等信息,所以要处理下,否则里面读不出视频总时间长度,视频取名为video.flv,如果你想改名字,到代码里改去,com文件夹里有个类是我写的来用于显示(00:00)时间格式的,另外一个是TWEENLITE,你懂的,,,这只是个简单播放器,如果你想要更复杂的,可以自己去添加,有不足的地方还请高手指出,为了学习,代码写的很凌乱,还望见谅。