当前位置:文档之家› flex视频播放器(支持rtmp协议)开发代码

flex视频播放器(支持rtmp协议)开发代码

Flex视频播放器(支持rtmp协议)开发代码开发工具:flash builder4.5 + red5服务器建议参考之前阶段代码:(1)flex视频播放器开发初级阶段代码:/detail/ll_jj_yy/(2)支持rtmp协议,播放red5服务器上的flv视频文件.直接来代码:<?xml version="1.0" encoding="utf-8"?><s:Application xmlns:fx="/mxml/2009"xmlns:s="library:///flex/spark"xmlns:mx="library:///flex/mx"minWidth="955" minHeight="600"creationComplete="init();"><fx:Script><![CDATA[import flash.utils.setTimeout;import mx.controls.Alert;import mx.controls.sliderClasses.Slider;import mx.events.SliderEvent;[Embed(source="assets/videoIco/play_small.jpg")][Bindable]private var playClass:Class;//播放图标样式[Embed(source="assets/videoIco/pause.jpg")][Bindable]private var pauseClass:Class;//暂停图标样式[Embed(source="assets/videoIco/sound1.jpg")][Bindable]private var sound:Class;//声音样式2(静音)[Embed(source="assets/videoIco/sound2.jpg")][Bindable]private var sound1:Class;//声音样式1[Bindable]private var _videoURL:String="rtmp://localhost:1935/live";//媒体路径private var _videoName:String="1.flv";//播放视频名称private var isPause:Boolean=false;//暂停状态private var isSound:Boolean=true;//声音状态private var isFullScreen:Boolean=false;//是否是全屏[Bindable]private var tmpSound:SoundTransform;//临时声音大小[Bindable]private var _playPosition:Number;//播放进度private var _duration:Number;//定义播放时间private var _nc:NetConnection;private var _inNs:NetStream;private var customClient:Object=new Object();private var vi:Video;private var flag:Boolean=false;//开始播放标志private function init():void{_nc=new NetConnection();//建立NetConnection对象_nc.addEventListener(_STATUS,onNetStatusHandler);_nc.client=this;_nc.connect(_videoURL);customClient.onMetaData=function(infoObject:Object):void{vi.width=vdisplay.width;vi.height=vdisplay.height;vi.smoothing=true;_duration=infoObject.duration;//获取视频持续时间t_sh.maximum=_duration;//关联进度条最大值与视频总持续时间th_sound.value=tmpSound.volume;};}private function onNetStatusHandler(evt:NetStatusEvent):void{ switch(.code){case"NetConnection.Connect.Success":// Alert.show("连接RED5服务器成功!");break;case"NetStream.Play.Start":trace("NetStream.Play.Start:");break;case"NetStream.Play.StreamNotFound":trace("Unable to locate:"+_videoURL);break;default:trace(.code);}}public function onBWCheck(...arg):void{}public function onBWDone(...arg):void{}public function playButton():void{//Alert.show(vdisplay.totalTime.toString());if(!isPause){if(!flag){if(_nc==null){init();}_inNs=new NetStream(_nc);_inNs.addEventListener(_STATUS,onNetStatusHandler);_inNs.client=customClient;vi=new Video();vi.attachNetStream(_inNs);vdisplay.addChild(vi);_inNs.play(_videoName);flag=true;///已经开始播放标志// Alert.show(tmpSound.volume.toString());tmpSound=_inNs.soundTransform;addEventListener(Event.ENTER_FRAME,onEnterFrame);}else_inNs.resume();playBtn.source=pauseClass;isPause=true;}else{_inNs.pause();playBtn.source=playClass;isPause=false;}}//时间格式操作// 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;// }private function formatTime(time:Number):String{var min:Number=Math.floor(time/60);var sec:Number=Math.floor(time%60);vartimeResult:String=(min<10?"0"+min.toString():min.toString())+":"+(sec<10?"0"+sec.toString():sec.toString());return timeResult;}private function onEnterFrame(event:Event):void{if(_duration>0&&_inNs.time>0){t_sh.value=_inNs.time;lbtime.text=formatTime(_inNs.time)+"/"+formatTime(_duration);}if(formatTime(_inNs.time)==formatTime(_duration)){if(!flag){removeEventListener(Event.ENTER_FRAME,onEnterFrame);_inNs.close();playBtn.source=playClass;t_sh.value=0;vdisplay.source="";lbtime.text="";isPause=false;isSound=false;}setTimeout(function():void{flag=false;},1000);}}private function display():void{if(!isFullScreen){stage.fullScreenSourceRect=new Rectangle(vdisplay.x,vdisplay.y,vdisplay.width,vdisplay.height);stage.displayState=StageDisplayState.FULL_SCREEN;isFullScreen=true;}else{stage.displayState=StageDisplayState.NORMAL;isFullScreen=false;}}private function stopButton():void{_inNs.close();t_sh.value=0;lbtime.text="";removeEventListener(Event.ENTER_FRAME,onEnterFrame);playBtn.source=playClass;isPause=false;flag=false;}private function closeSound():void{if(isSound){if(_inNs!=null){var tmpSound1:SoundTransform=new SoundTransform(0);_inNs.soundTransform=tmpSound1;closeImg.source=sound;th_sound.enabled=false;isSound=false;}}else{if(_inNs!=null){_inNs.soundTransform=tmpSound;closeImg.source=sound1;th_sound.enabled=true;isSound=true;}}}private function sound_thumbChanges(event:SliderEvent):void{ tmpSound.volume=th_sound.value;_inNs.soundTransform=tmpSound;}private function thumbPress(event:SliderEvent):void{_inNs.pause();}private function thumbRelease(event:SliderEvent):void{_inNs.seek(t_sh.value);_inNs.resume();}private function thumbChanges(event:SliderEvent):void{_playPosition=t_sh.value;_inNs.seek(_playPosition);}private function dataTipFormat(time:Number):String{return formatTime(time);}]]></fx:Script><s:Panel x="93" y="19" width="524" height="485"><mx:VideoDisplay id="vdisplay" x="27" y="10" width="480" height="360"autoPlay="false"doubleClickEnabled="true"doubleClick="display();"/><s:HGroup width="473" verticalAlign="middle" x="17" y="395"><s:Image source="{playClass}" click="playButton();" id="playBtn" buttonMode="true"/><s:Image source="@Embed('assets/videoIco/stop.jpg')" click="stopButton();" buttonMode="true"/><s:Label id="lbtime" width="112"/><s:Line height="0" width="80"/><s:Image source="{sound1}" click="closeSound();" id="closeImg" buttonMode="true"/><mx:HSlider width="96" id="th_sound" minimum="0" maximum="1"change="sound_thumbChanges(event)"value="0.35"/><mx:Button label="全屏" click="display();" cornerRadius="20"/> </s:HGroup><mx:HSlider id="t_sh" y="378" width="490" height="9" change="thumbChanges(event)"dataTipFormatFunction="dataTipFormat" horizontalCenter="6"minimum="0" showTrackHighlight="true"thumbPress="thumbPress(event)"thumbRelease="thumbRelease(event)"/></s:Panel></s:Application>Hope it helps!建议查看--Flex支持rtmp协议简单播放:/detail/ll_jj_yy/4856506可部署web访问。

相关主题