Flex3播放器(版本1)
<!-- 原创(参考:帮助文档)
???? Product:Uyang
???? http://www.uncool.cn/blogs
???? 基本实现了一个播放器应该有的功能,进度条,时间显示,声音控制,如果你喜欢可以再加上其他的功能;
???? 这里有一个扩展内容为XML扩展,想深入下去的朋友,可以用我这个读取XML的方法,然后把值赋给LIST,
???? 比如:<mx:list/> 通过itemRenderer来排列读取的内容。???
-->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
creationComplete="playinit()">
<!--扩展XML内容 creationComplete="videoserver.send()" -->
??? <mx:Script>
??? <![CDATA[
??? ?? import mx.events.SliderEvent;
??? ?? import mx.events.VideoEvent;
??? ?? import mx.collections.ArrayCollection;
??? ?? import mx.rpc.events.ResultEvent;
??? ??
??? ?? //用来收集XML反馈来的数组;
??? ?? private var _video:ArrayCollection;
??? ??
??? ?? private var isplaying:Boolean=false;
??? ??
??? ?? //设置你要播放的FLV文件地址;
??? ?? private var video_url:String = "tef.flv";
??? ??
??? ?? private var playPosition:Number;
??? ??
??? ?? private var soundPosition:Number;
??? ??
??? ?? /*读取XML扩展内容
??? ?? 分析HTTPSERVICE得到的结果,然后把结果赋值给_video;
??? ?? 如果想知道更加的清楚,可以?? trace(_video)看看;
??? ?? videos.xml的格式为:
??? ?? <?xml version="1.0" encoding="utf-8" ?>
??? <videos>
????? <video filename="xxx" />
???? </videos>
??? ?? private function readXml(event:ResultEvent):void{
??? ?? _video = event.result.videos.video;
???????????? }???
??? ?? */
??? ?? private function playinit():void{
??? ??? hs.enabled=false;
??? ?? }
??? ??
??? ?? //播放按钮点击后事件:播放视频,同时分析是否播放来调整BUTTON上的标签,显示为PLAY或者PAUSE;
??? ?? //并监听播放器
??? ?? private function flvplay(event:Event):void{
??? ??? flv_video.source=video_url;
??? ??? hs.enabled=true;
??? ??? //xml扩展,读取第一个XML第一个文件示例
??? ??? //flv_video.source="videos/"+_video.getItemAt(0).filename;
??? ??? //trace(_video.getItemAt(0).filename);
??? ??? if(isplaying){
??? ???? flv_video.pause();
??? ???? play_btn.label="Play"
??? ??? }else {
??? ???? flv_video.play();
??? ???? play_btn.label="Pause"
??? ??? }
??? ??? isplaying = !isplaying;
??? ??? flv_video.addEventListener(VideoEvent.PLAYHEAD_UPDATE, progressHandler);???????
??? ?? }
??? ??
??? ?? //播放器监听事件,从FLV_VIDEO里不停的得到值然后传输给HSLIDER
??? ?? private function progressHandler(event:VideoEvent):void{
??? ??? hs.value=flv_video.playheadTime;
??? ?? }
??? ??
??? ?? //拉动进度条
??? ?? private function thumbPress(event:SliderEvent):void{
??? ??? flv_video.pause();
??? ?? }
??? ?? //进度条改变后,得到的值赋予PLAYPOSITION;
??? ?? private function thumbChanges(event:SliderEvent):void{
??? ??? if(flv_video.playheadTime == -1){
????? hs.value = 0;
????? return;
???? }
??? ??? playPosition = hs.value;
??? ?? }
??? ??
??? ?? //放开进度条,再把PLAYPOSITION的值发给播放器;
??? ?? private function thumbRelease(event:SliderEvent):void{
??? ??? flv_video.playheadTime = playPosition;
??? ??? if(isplaying){
??? ???? flv_video.play();
??? ??? }else{
??? ???? flv_video.pause();
??? ??? }
??? ?? }
??? ?? //声音音量控制??? ???? ??
??? ?? private function sound_thumbChanges(event:SliderEvent):void{
??? ??? soundPosition = hs_sound.value;
??? ?? }
??? ??
???
??? ?? private function sound_thumbRelease(event:SliderEvent):void{
??? ??? flv_video.volume = soundPosition;
??? ?? }
??? ??
??? ?? //格式化时间
??? ?? private function formatTimes(value:int):String{
??? ??? var result:String = (value % 60).toString();
??? ???
????????? if (result.length == 1){
????????????? result = Math.floor(value / 60).toString() + ":0" + result;
????????? } else {
????????????? result = Math.floor(value / 60).toString() + ":" + result;
????????? }
????????? return result;
??? ?? }
??? ??
??? ]]>
??? </mx:Script>
??? <!--通过HTTPSERVICE来分析XML,然后得出RESULT,结果的反馈在SCRIPT里 -->
??? <!--此为读取XML扩展内容
??? <mx:HTTPService id="videoserver" url="assets/videos.xml" result="readXml(event)"/>
???? -->
??? <!--主要的视频播放窗口 设置ID为FLVVIDEO,这个很重要,其他坐标可以随自己喜欢 -->
<mx:VideoDisplay?? id="flv_video" x="322" y="97" width="320" height="240"/>
<!--播放器的播放进度条,用FLEX自带的HSLIDER来表现播放进度,同时可以拖动影片 -->
<mx:HSlider id="hs" x="322" y="340" minimum="0" maximum="{flv_video.totalTime}"
?? thumbPress="thumbPress(event)"
?? thumbRelease="thumbRelease(event)"
???? change="thumbChanges(event)" />
<!--播放器声音控制 -->
<mx:HSlider id="hs_sound" x="560" y="340" width="80"
???? minimum="0" maximum="1"
?? thumbRelease="sound_thumbRelease(event)"
???? change="sound_thumbChanges(event)"
???? value="{flv_video.volume}" />
<!-- 播放按钮,根据是否在播放,按钮显示为:PLAY 或者PAUSE -->
<mx:Button id="play_btn" x="322" y="365" click="flvplay(event)" label="Play"/>
<!-- 播放按钮,停止播放影片 -->
<mx:Button id="stop_btn" label="Stop" x="439" y="365"
?? click="flv_video.stop();play_btn.label='Play';hs.enabled=false;" />
<!-- 时间显示 -->
<mx:Label x="480" y="341" id="playtime"
?? text="{formatTimes(flv_video.playheadTime)} : {formatTimes(flv_video.totalTime)}"
?? color="#ffffff"/>
<mx:Label x="322" y="62" text="UYang Pruduct"/>
</mx:Application>