您的位置:首页 > 运维架构 > 网站架构

flash swf接受外部参数 视频网站播放原理

2017-06-30 16:08 302 查看
转载自:

http://blog.sina.com.cn/s/blog_5e83fce60100r663.html

<object id="bcastr" data="camnpr.swf?xml=xml/camnpr.xml" type="application/x-shockwave-flash"
width="650" height="285">
<param name="movie" value="camnpr.swf?xml=xml/camnpr.xml" />
<param name="wmode" value="transparent">
</object>

其中关键的部分是: data="camnpr.swf?xml=xml/camnpr.xml"
<param name="movie" value="camnpr.swf?xml=xml/camnpr.xml" />这部分可以不带参数等价于
<param name="movie" value="camnpr.swf" />


然后在swf的源文件.fla文件的动作F9里用“_root.参数名”来获取外部传过来的参数值,此例中访问参数值是:_root.xml

对此,类似视频网站上的播放视频的前后各加一个广告视频(.flv),应用原理可以由此推出:

swf文件接受3个参数;

<object id="bcastr" data="camnpr.swf?Ad1=1.flv&Content=con.flv&Ad2=2.flv&jump=0" type="application/x-shockwave-flash"
width="650" height="285">
<param name="movie" value="bcastr.swf" />
<param name="wmode" value="transparent">
</object>


其中“jump=0”表示第一个广告不可以跳过,jump=1表示可以跳过。

接下来要在.fla里边处理外部参数的定义,也就是要做好一个视频加载的flash框架;

首先建3个层,从上到下分别是:AS层,屏幕层,控制元件层。

1:新建视频放在屏幕层,命名实例名称为“my_video”.

2:建控制按钮,放在控制元件层

分别是 播放按钮(实例名称为“btn_play”)上一首(实例名称为“btn_prev”)下一首(实例名称为“btn_next”)

音量控制包括 静音开关(实例名称为“btn_vol”)音量滑块(实例名称为“vol_mc”)

其他控制元件后面写AS代码时再具体说明,以上元件根据个人喜好制作,这里就不多说了。

3:该在AS层写代码了:

//声明
var p:Number = 0;
var total_num:Number = 0;
var title_array:Array = new Array();
var path_array:Array = new Array();
var uid_array:Array = new Array();
//解析XML
var my_xml:XML = new XML();
my_xml.ignoreWhite = true;
my_xml.load("laoge.xml");
my_xml.onLoad = function(success) {
if (success) {
for (var d:Number = 0; d<this.firstChild.childNodes.length; d++) {
title_array.push(this.firstChild.childNodes[d].attributes.title);
path_array.push(this.firstChild.childNodes[d].attributes.path);
uid_array.push(this.firstChild.childNodes[d].attributes.uid);
}
//获取总曲目数
total_num = this.firstChild.childNodes.length;
//trace("total_num is :"+total_num);
play_func();
} else {
trace("加载出错!");
}
};
//首先视频初始化
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
my_video.smoothing = true;
my_video.attachVideo(ns);
ns.setBufferTime(1);
//然后获取各种所需信息
var flv_dur:Number = 0;
ns.onMetaData = function(infoObject:Object) {
flv_dur = infoObject.duration;
time();
};
function time() {
onEnterFrame = function () {
//音量信息
var vol:Number = _root.mc.vol_mc._x-350;
my_sound.setVolume(Math.floor(vol*2.5));
_root.mc.volume_txt.text = (Math.floor(vol*2.5));
//这里的volume_txt是显示音量大小的数值,可以在音量滑块附近建个动态文本实例名 称为“volume_txt”
//时间信息
var ns_seconds:Number = ns.time;
var minutes = Math.floor(ns_seconds/60);
var seconds = Math.floor(ns_seconds`);
var total_min = Math.floor(flv_dur/60);
var total_sec = Math.floor(flv_dur`);
//让时间显示格式为"00:00"
minutes<10 ? (minutes="0"+minutes) : NULL;
seconds<10 ? (seconds="0"+seconds) : NULL;
total_min<10 ? (total_min="0"+total_min) : NULL;
total_sec<10 ? (total_sec="0"+total_sec) : NULL;
//显示时间信息  这里要建2个显示时间的动态文本,实例名称分别为:当前播放时间videotime_left 总播放时间
videotime_right
_root.mc.videotime_left.text = minutes+":"+seconds;
_root.mc.videotime_right.text = total_min+":"+total_sec;
//因播放器支持全屏播放,以下代码为按ESC退出全屏
if (Key.isDown(Key.ESCAPE)) {
my_video._x = 10;
my_video._y = 20;
my_video._width = 400;
my_video._height = 300;
}
};
}
//播放进度条及加载百分比显示 建进度滑块实例名称为my_mc百分比显示的动态文本实例名称为yjz
function videoStatus() {
var videoTotal:Number = ns.bytesTotal;
var videoLoaded:Number = ns.bytesLoaded;
percent_Loaded = videoLoaded/videoTotal*100;
_root.mc.yjz.text = "已加载"+int(percent_Loaded)+"%";
_root.mc.my_mc._x = int(ns.time/flv_dur*395);
//395这个数值根据你滑动范围具体调整
}
//播放函数
function play_func() {
ns.stop();
//trace(uid_array[p]);
ns.play(path_array[p]);
//trace("The flv path is:"+path_array[p]);
//trace(p);
_root.mc.how_txt.text = title_array[p];
_root.mc.num_txt.text = uid_array[p]+" / "+total_num;
}
//连续播放
ns.onStatus = function(infoObject) {
if (infoObject.code == "NetStream.Play.Stop") {
//trace("播放完成");
if (p<total_num-1) {
p++;
} else {
P = 0;
}
play_func();
}
};
//音量控制区
this.createEmptyMovieClip("flv_audio", this.getNextHighestDepth());
flv_audio.attachAudio(ns);
var my_sound:Sound = new Sound(flv_audio);
_root.mc.vol_mc.onPress = function() {
this.startDrag(false, 350, 31.2, 390, 31.2);
};
_root.mc.vol_mc.onRelease = _root.mc.vol_mc.onReleaseOutside=function () {
stopDrag();
};
//按钮功能区
//播放按钮
_root.mc.btn_play.onRelease = function() {
this.id = !this.id;
this.id ? (ns.pause(true)) and (how_txt.text="已暂停") : (ns.pause(false)) and (how_txt.text="正在播放");
};
//下一首.这里要判断当前播放曲目"p"与总曲目数的关系
_root.mc.btn_next.onRelease = function() {
if (p<total_num-1) {
p++;
} else if (p=total_num-1) {
Null;
} else {
Null;
}
play_func();
};
//上一首.判断当前播放曲目"p"是否是第一首.
_root.mc.btn_prev.onRelease = function() {
p != 0 ? p-- : null;
play_func();
};


4:打开记事本编写以下代码:保存为list.xml文件放在同一文件夹里

<?xml version="1.0" encoding="UTF-8"?>
<flv>
<vid="1" title="爆!被央视《走近科学》删除的神秘视频" path="http://madrid.6rooms.com/h/2c/dc/0c91662336766.flv" uid="1"/>
<vid="2" title="赵本山徒弟的爆笑演讲" path="http://barcelona.6rooms.com/27/c0/caf21469240428.flv" uid="2"/>
<vid="3" title="小沈阳" path="http://barcelona.6rooms.com/e7/9b/37171049245226.flv" uid="3"/>
<vid="4" title="史上最强情人节之《天生绝
配"path="http://music4.tool.hexun.com/Save/Video/2008/0305/1581/M_14A59BA568F5C383.FLV" uid="4"/>
<vid="5" title="小沈阳" path="http://barcelona.6rooms.com/e7/9b/37171049245226.flv" uid="5"/>
</flv>


5:制作个全屏按钮放在合适位置,按钮上写以下代码

on (release) {
if (Stage["displayState"] == "normal") {
Stage["displayState"] = "fullScreen";
_root.my_video._x = -_root.cc._x-10;
_root.my_video._y = -_root.cc._y-20;
_root.my_video._width = 1024;
_root.my_video._height = 788;
} else {
Stage["displayState"] = "normal";
_root.my_video._x = _root.cc._x-477.1;
_root.my_video._y = _root.cc._y-54.2;
_root.my_video._width = 400;
_root.my_video._height = 300;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: