简易flash播放器

import flash.external.*;
//--------------------------------------------------------------------------
// 初始化可能用到的变量
//--------------------------------------------------------------------------
// 设定默认打开就播放的文件名(如果在html中没有设置)
// 你可以改变'video.flv'
if (!_root.file) {
file = 'video';
} else {
file = _root.file;
}
if (_root.image) {
imageStr = _root.image;
} else {
imageStr = file+".jpg";
}
//定义全局变量
var isStarted = false;
var Size = false;
var flv_width;
var flv_height;
//监听器注册
var stageListener:Object = new Object();
stageListener.onResize = function() {
changeFlash();
};
Stage.addListener(stageListener);
//--------------------------------------------------------------------------
// 流设置及函数
//--------------------------------------------------------------------------
// 创建并设置网络流
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.setBufferTime(30);
// 创建并设置声音对像
this.createEmptyMovieClip("snd", 0);
snd.attachAudio(ns);
audio = new Sound(snd);
audio.setVolume(80);
// 绑定显示
videoDisplay.attachVideo(ns);
// 从网络流中获取状态消息
ns.onStatus = function(obj:Object) {
if (obj.code == "NetStream.Play.Stop") {
// 影片完成后返回并暂停
ns.seek(0);
if (_root.repeat) {
return;
}
ns.pause();
playBut._visible = true;
pauseBut._visible = false;
videoDisplay._visible = false;
playText.text = "重新播放";
goBtn._visible = true;
}
};
//--------------------------------------------------------------------------
// 调整大小并定位所有项目
//--------------------------------------------------------------------------
changeFlash();
//然后获得flv的固有尺寸,并调用以上函数改变它 
//此处理函数在调用 my_ns.play() 方法后而在视频播放头前进之前触发 
ns.onMetaData = function(obj:Object) {
//获得FLV文件中嵌入的描述性信息,这里获得宽/高 
flv_width = obj.width;
flv_height = obj.height;
//改变尺寸 
changeVideo();
// 从网络流中获取时间元素
this.totalTime = obj.duration;
};
function changeVideo() {
w = flv_width;
h = flv_height;
trace(Size)
if (Size!=undefined) {
if (w/h>Stage.width/(Stage.height-35)) {
//宽度优先算法
videoDisplay._width = imageClip._width=Stage.width;
videoDisplay._height = imageClip._height=h/w*videoDisplay._width;
videoDisplay._x = imageClip._x=Stage.width/2-videoDisplay._width/2;
videoDisplay._y = imageClip._y=(Stage.height-35)/2-videoDisplay._height/2;
} else {
//高度优先算法
videoDisplay._height = imageClip._height=Stage.height-35;
videoDisplay._width = imageClip._width=w/h*videoDisplay._height;
videoDisplay._y = imageClip._y=(Stage.height-35)/2-videoDisplay._height/2;
videoDisplay._x = imageClip._x=Stage.width/2-videoDisplay._width/2;
}
} else {
w =w * 2;
h =h * 2;
//原始尺寸算法 
videoDisplay._width = imageClip._width=w;
videoDisplay._height = imageClip._height=h;
videoDisplay._x = imageClip._x=Stage.width/2-w/2;
videoDisplay._y = imageClip._y=(Stage.height-35)/2-h/2;
}
}
//原始大小显示
function changeFlash() {
sw = Stage.width;
sh = Stage.height;
videoBg._width = sw;
videoBg._height = sh-35;
playText._x = sw/2-120;
playText._y = (sh-35)/2+40;
goBtn._x = sw/2-40;
goBtn._y = (sh-35)/2-50;
buttonBg._x = 0;
buttonBg._y = sh-35;
buttonBg._width = sw;
leftbar._x = 10;
leftbar._y = sh-32;
bgbar._x = 10+8;
bgbar._y = sh-32;
bgbar._width = sw-36;
rightbar._x = sw-10-8;
rightbar._y = sh-32;
pauseBut._x = playBut._x=20;
pauseBut._y = playBut._y=sh-27;
stopBut._x = 52;
stopBut._y = sh-24;
PB._x = sw-180;
PB._y = sh-22;
volumeBar._x = sw-102;
volumeBar._y = sh-24;
changeBut._x = sw-38;
changeBut._y = sh-24;
oriBut._x = sw-200;
oriBut._y = sh-24;
progressBar._x = 80;
progressBar._y = sh-16;
progressBar._width = sw-275;
Handle._y = sh-15;
if (!isStarted) {
imageClip._x = 0;
imageClip._y = 0;
imageClip._width = Stage.width;
imageClip._height = Stage.height-35;
} else {
changeVideo();
}
}
//显示播放时间
function showTimeTxt() {
PB.timeTxt.text = (((((fixNum(Math.floor(ns.time/60))+":")+fixNum(Math.floor(ns.time%60)))+" / ")+fixNum(Math.floor(ns.totalTime/60)))+":")+fixNum(Math.floor(ns.totalTime%60));
}
function fixNum(num) {
return (((num<10) ? ("0"+num) : (num)));
}
//--------------------------------------------------------------------------
// 控制条功能
//--------------------------------------------------------------------------
// 播放影片并隐藏播放按钮
function playMovie() {
if (!isStarted) {
ns.play(file+".flv");
ns.setBufferTime(10);
playText.text = "影片加载中";
isStarted = true;
} else {
ns.pause();
}
pauseBut._visible = true;
playBut._visible = false;
goBtn._visible = false;
videoDisplay._visible = true;
}
// 影片暂停并隐藏暂停按钮
function pauseMovie() {
ns.pause();
playBut._visible = true;
goBtn._visible = true;
pauseBut._visible = false;
}
// 点击影片的动作
videoBg.onPress = function() {
if (pauseBut._visible == false) {
playMovie();
} else {
pauseMovie();
}
};
// 点击暂停按钮的动作
pauseBut.onPress = function() {
pauseMovie();
};
// 点击播放按钮的动作
playBut.onPress = function() {
playMovie();
};
goBtn.onPress = function() {
playMovie();
};
// 点击停止按钮的动作
stopBut.onPress = function() {
ns.seek(0);
//搜寻从0开始播放 
ns.pause(true);
pauseBut._visible = false;
playBut._visible = true;
goBtn._visible = true;
//参数true表示暂停,若是false则表示从暂停变为继续播放,若没有参数则为在暂停/播放中切换。
};
//全屏/退出按钮(鼠标按下动作)
changeBut.onPress = function() {
if (Stage["displayState"] == "normal") {
Stage["displayState"] = "fullscreen";
} else {
Stage["displayState"] = "normal";
}
};
//--------------------------------------------------------------------------
// 影片进度控制
//--------------------------------------------------------------------------
// 影片载入进度
progressBar.onEnterFrame = function() {
loaded = this._parent.ns.bytesLoaded;
total = this._parent.ns.bytesTotal;
if (loaded == total && loaded>1000) {
this.loa._xscale = 100;
delete this.onEnterFrame;
} else {
this.loa._xscale = int(loaded/total*100);
}
};
// 影片播放进度
progressBar.tme.onEnterFrame = function() {
this._xscale = ns.time/ns.totalTime*100;
Handle._x = this._xscale/100*progressBar._width+85;
showTimeTxt();
};
// 鼠标托动进度条播放(鼠标按下动作)
progressBar.loa.onPress = function() {
this.onEnterFrame = function() {
scl = (this._xmouse/this._width)*(this._xscale/100);
if (scl<0.02) {
scl = 0;
}
ns.seek(scl*ns.totalTime);
showTimeTxt();
};
};
// 鼠标托动进度条播放(鼠标释放动作)
progressBar.loa.onRelease = progressBar.loa.onReleaseOutside=function () {
delete this.onEnterFrame;
pauseBut._visible == false ? videoDisplay.pause() : null;
};
//--------------------------------------------------------------------------
// 影片音量控制
//--------------------------------------------------------------------------
// 音量控制(鼠标按下动作)
volumeBar.back.onPress = function() {
this.onEnterFrame = function() {
var xm = this._xmouse;
if (xm>=0 && xm<=40) {
this._parent.mask._width = this._xmouse;
this._parent._parent.audio.setVolume(this._xmouse*2.5);
}
};
};
// 音量控制(鼠标释放动作)
volumeBar.back.onRelease = volumeBar.back.onReleaseOutside=function () {
delete this.onEnterFrame;
};
//声音按钮(鼠标按下动作)
volumeBar.icn1.onPress = function() {
volumeBar.icn1._visible = false;
volumeBar.icn0._visible = true;
vSite = this._parent.mask._width;
this._parent.mask._width = 0;
this._parent._parent.audio.setVolume(0);
};
//静音按钮(鼠标按下动作)
volumeBar.icn0.onPress = function() {
volumeBar.icn0._visible = false;
volumeBar.icn1._visible = true;
this._parent.mask._width = vSite;
this._parent._parent.audio.setVolume(vSite*2.5);
};
//--------------------------------------------------------------------------
// 开始播放影片
//--------------------------------------------------------------------------
pauseBut._visible = false;
volumeBar.icn0._visible = false;
imageClip.loadMovie(imageStr);
if (_root.autoStart) {
playMovie();
}
// 播放影片并隐藏播放按钮(flv)                
function playVideo(url) {
file = url;
isStarted = false;
playMovie();
}
//JS调用FLASH函数

ExternalInterface.addCallback("playVideo", null, playVideo);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值