FlashAS3教程:组件的组合运用制作FLV播放器
本例为FlashAS3实例教程,主要学习组件的组合运用制作FLV播放器,通过本教程将实现以下功能,按前进、后退按钮可选择播放,拖动滑块可调节音量,希望能给朋友们带来帮助~~
实现的功能:按前进、后退按钮可选择播放,拖动滑块可调节音量。
测试环境:FlashCS4
1、组织界面:
新建Flash文档文件,命名保存。
打开组件面板,点开UserInterface组。把Label拖到场景中,命名为:postionLabel,拖ProgressBar到场景中,命名为:positionBar,拖Slider到场景中,命名为:volumeSlider,点开Video组。拖PlayButton到场景中,命名为:playButton,拖PauseButton到场景中,命名为:pauseButton,拖StopButton到场景中,命名为:stopButton,拖BackButton到场景中,命名为:backButton,拖ForwardButton到场景中,命名为:forwardButton。组织好位置如图1图2:
FlashAS3教程:组件的组合运用制作FLV播放器_中国教程网
FlashAS3教程:组件的组合运用制作FLV播放器_中国教程网
2、创建VideoJukebox.as文档文件,代码如下:(代码可直接拷贝)
package{
importfl.controls.;
importfl.events.SliderEvent;
importflash.display.MovieClip;
importflash.display.Sprite;
importflash.events.Event;
importflash.events.MouseEvent;
importflash.events.NetStatusEvent;
importflash.events.TimerEvent;
importflash.media.SoundTransform;
importflash.media.Video;
importflash.net.NetConnection;
importflash.net.NetStream;
importflash.net.URLLoader;
importflash.net.URLRequest;
importflash.utils.Timer;
publicclassVideoJukeboxextendsSprite{
/
Theamountoftimebetweencallstoupdatetheplayheadtimer,in
milliseconds.
/
privateconstPLAYHEAD_UPDATE_INTERVAL_MS:uint=10;
/
ThepathtotheXMLfilecontainingthevideoplaylist.
/
privateconstPLAYLIST_XML_URL:String="playlist.xml";
/
TheclientobjecttousefortheNetStreamobject.
/
privatevarclient:Object;
/
Theindexofthecurrentlyplayingvideo.
/
privatevaridx:uint=0;
/
Acopyofthecurrentvideo''smetadataobject.
/
privatevarmeta:Object;
privatevarnc:NetConnection;
privatevarns:NetStream;
privatevarplaylist:XML;
privatevart:Timer;
privatevaruldr:URLLoader;
privatevarvid:Video;
privatevarvideosXML:XMLList;
/
TheSoundTransformobjectusedtosetthevolumefortheNetStream.
/
privatevarvolumeTransform:SoundTransform;
/
Constructor
/
publicfunctionVideoJukebox(){
//Initializetheuldrvariablewhichwillbeusedtoloadtheexternal
//playlistXMLfile.
uldr=newURLLoader();
uldr.addEhttp://www.edu800.cnventListener(Event.COMPLETE,xmlCompleteHandler);
uldr.load(newURLRequest(PLAYLIST_XML_URL));
}
/
OncetheXMLfilehasloaded,parsethefilecontentsintoanXMLobject,
andcreateanXMListforthevideonodesintheXML.
/
privatefunctionxmlCompleteHandler(event:Event):void{
playlist=XML(event.target.data);
videosXML=playlist.video;
main();
}
/
Themainapplication.
/
privatefunctionmain():void{
volumeTransform=newSoundTransform();
//CreatetheclientobjectfortheNetStream,andsetupacallback
//handlerfortheonMetaDataevent.
client=newObject();
client.onMetaData=metadataHandler;
nc=newNetConnection();
nc.connect(null);
//InitializetheNetSteamobject,addalistenerforthenetStatus
//event,andsettheclientfortheNetStream.
ns=newNetStream(nc);
ns.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
ns.client=client;
//InitializetheVideoobject,attachtheNetStram,andaddtheVideo
//objecttothedisplaylist.
vid=newVideo();
vid.x=20;
vid.y=75;
vid.attachNetStream(ns);
addChild(vid);
//Beginplaybackofthefirstvideo.
playVideo();
//InitializetheTimerobjectandsetthedelayto
//PLAYHEAD_UPDATE_INTERVAL_MSmilliseconds.
t=newTimer(PLAYHEAD_UPDATE_INTERVAL_MS);
t.addEventListener(TimerEvent.TIMER,timerHandler);
//ConfigurethepositionBarProgressBarinstanceandsetthemodeto
//MANUAL.Progressbarvalueswillbeexplicitlysetusingthe
//setProgress()method.
positionBar.mode=ProgressBarMode.MANUAL;
//ConfigurethevolumeSliderSlidercomponentinstance.Themaximum
//valueissetto1becausethevolumeintheSoundTransformobject
//issettoanumberbetween0and1.ThesnapIntervalandtickInterval
//propertiesaresetto0.1whichallowsuserstosetthevolumeto
//0,0.1-0.9,1.0whichallowsuserstoincrementordecrementthe
//volumeby10%.
volumeSlider.value=volumeTransform.volume;
volumeSlider.minimum=0;
volumeSlider.maximum=1;
volumeSlider.snapInterval=0.1;
volumeSlider.tickInterval=volumeSlider.snapInterval;
//SettingtheliveDraggingpropertytotruecausestheSlider
//instance''schangeeventtobedispatchedwhenevertheslideris
//moved,ratherthanwhentheuserreleasesthesliderthumb.
volumeSlider.liveDragging=true;
volumeSlider.addEventListener(SliderEvent.CHANGE,volumeChangeHandler);
//ConfigurethevariousButtoninstances.EachButtoninstanceuses
//thesameclickhandler.
playButton.addEventListener(MouseEvent.CLICK,buttonClickHandler);
pauseButton.addEventListener(MouseEvent.CLICK,buttonClickHandler);
stopButton.addEventListener(MouseEvent.CLICK,buttonClickHandler);
backButton.addEventListener(MouseEvent.CLICK,buttonClickHandler);
forwardButton.addEventListener(MouseEvent.CLICK,buttonClickHandler);
}
/
EventlistenerforthevolumeSliderinstance.Calledwhentheuser
changesthevalueofthevolumeslider.
/
privatefunctionvolumeChangeHandler(event:SliderEvent):void{
//SetthevolumeTransform''svolumepropertytothecurrentvalueofthe
//SliderandsettheNetStreamobject''ssoundTransformproperty.
volumeTransform.volume=event.value;
ns.soundTransform=volumeTransform;
}
/
Eventlistenerforthensobject.Calledwhenthenetstream''sstatus
changes.
/
privatefunctionnetStatusHandler(event:NetStatusEvent):void{
try{
switch(event.info.code){
case"NetStream.Play.Start":
//IfthecurrentcodeisStart,startthetimerobject.
t.start();
break;
case"NetStream.Play.StreamNotFound":
case"NetStream.Play.Stop":
//IfthecurrentcodeisStoporStreamNotFound,stop
//thetimerobjectandplaythenextvideointheplaylist.
t.stop();
playNextVideo();
break;
}
}catch(error:TypeError){
//Ignoreanyerrors.
}
}
/
Eventlistenerforthensobject''sclientproperty.Thismethodiscalled
whenthenetstreamobjectreceivesmetadatainformationforavideo.
/
privatefunctionmetadataHandler(metadataObj:Object):void{
//Storethemetadatainformationinthemetaobject.
meta=metadataObj;
//ResizetheVideoinstanceonthedisplaylistwiththevideo''swidth
//andheightfromthemetadataobject.
vid.width=meta.width;
vid.height=meta.height;
//RepositionandresizethepositionBarprogressbarbasedonthe
//currentvideo''sdimensions.
positionBar.move(vid.x,vid.y+vid.height);
positionBar.width=vid.width;
}
/
RetrievethecurrentvideofromtheplaylistXMLobject.
/
privatefunctiongetVideo():String{
returnvideosXML[idx].@url;
}
/
Playthecurrentlyselectedvideo.
/
privatefunctionplayVideo():void{
varurl:String=getVideo();
ns.play(url);
}
/
Decreasethecurrentvideoindexandbeginplaybackofthevideo.
/
privatefunctionplayPreviousVideo():void{
if(idx>0){
idx--;
playVideo();
//MakesurethepositionBarprogressbarisvisible.
positionBar.visible=true;
}
}
/
Increasethecurrentvideoindexandbeginplaybackofthevideo.
/
privatefunctionplayNextVideo():void{
if(idx<(videosXML.length()-1)){
//Ifthisisnotthelastvideointheplaylistincreasethe
//videoindexandplaythenextvideo.
idx++;
playVideo();
//MakesurethepositionBarprogressbarisvisible.
positionBar.visible=true;
}else{
//Ifthisisthelastvideointheplaylistincreasethevideo
//index,clearthecontentsoftheVideoobjectandhidethe
//positionBarprogressbar.Thevideoindexisincreasedsothat
//whenthevideoends,clickingthebackButtonwillplaythe
//correctvideo.
idx++;
vid.clear();
positionBar.visible=false;
}
}
/
Clickhandlerforeachofthevideoplaybackbuttons.
/
privatefunctionbuttonClickHandler(event:MouseEvent):void{
//Useaswitchstatementtodeterminewhichbuttonwasclicked.
switch(event.currentTarget){
caseplayButton:
//Iftheplaybuttonwasclicked,resumethevideoplayback.
//Ifthevideowasalreadyplaying,thishasnoeffect.
ns.resume();
break;
casepauseButton:
//Ifthepausebuttonwasclicked,pausethevideoplayback.
//Ifthevideowasalreadyplaying,thevideowillbepaused.
//Ifthevideowasalreadypaused,thevideowillberesumed.
ns.togglePause();
break;
casestopButton:
//Ifthestopbuttonwasclicked,pausethevideoplayback
//andresettheplayheadbacktothebeginningofthevideo.
ns.pause();
ns.seek(0);
break;
casebackButton:
//Ifthebackbuttonwasclicked,playthepreviousvideoin
//theplaylist.
playPreviousVideo();
break;
caseforwardButton:
//Iftheforwardbuttonwasclicked,playthenextvideoin
//theplaylist.
playNextVideo();
break;
}
}
/
Eventhandlerforthetimerobject.Thismethodiscalledevery
PLAYHEAD_UPDATE_INTERVAL_MSmillisecondsaslongasthetimerisrunning.
/
privatefunctiontimerHandler(event:TimerEvent):void{
try{
//Updatetheprogressbarandlabelbasedontheamountofvideo
//thathasplayedback.
positionBar.setProgress(ns.time,meta.duration);
positionLabel.text=ns.time.toFixed(1)+"of"+meta.duration.toFixed(1)+"seconds";
}catch(error:Error){
//Ignorethiserror.
}
}
}
}
3、建一个xml文件,打开记事本输入下面内容:(此文件包涵3段flv地址,需要的话按下面格式添加)
在记事本中,选择【文件】【另存为】命令,输入文件名为:playlist.xml,在编码选项中选择“UTF-8”,单击【保存】按钮。如图3:
FlashAS3教程:组件的组合运用制作FLV播放器_中国教程网
用IE浏览器打开“playlist.xml”文件,如果能看到里面的内容,说明XML文件创建成功,如图4:
FlashAS3教程:组件的组合运用制作FLV播放器_中国教程网
4、返回到fla场景中,在属性面板类输入框中输入:VideoJukebox。
5、把VideoJukebox.as文档文件,playlist.xml文件,fla文件保存在同一目录下,测试。
|
|