您的当前位置:首页正文

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

2024-07-14 来源:步旅网
Flex视频播放器(支持rtmp协议)开发代码

开发工具:flash builder4.5 + red5服务器 建议参考之前阶段代码:

(1)flex视频播放器开发初级阶段代码:http://download.csdn.net/detail/ll_jj_yy/ (2)支持rtmp协议,播放red5服务器上的flv视频文件. 直接来代码:

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(NetStatusEvent.NET_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(evt.info.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(evt.info.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(NetStatusEvent.NET_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); var

timeResult: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); } ]]>

doubleClickEnabled=\"true\" doubleClick=\"display();\"/>

dataTipFormatFunction=\"dataTipFormat\" horizontalCenter=\"6\" minimum=\"0\" showTrackHighlight=\"true\" thumbPress=\"thumbPress(event)\" thumbRelease=\"thumbRelease(event)\" />

Hope it helps!

建议查看--Flex支持rtmp协议简单播放:http://download.csdn.net/detail/ll_jj_yy/4856506 可部署web访问

因篇幅问题不能全部显示,请点此查看更多更全内容