Flash Builder 开发视频播放器客户端_直播

基于NetConnection,NetStream实现基于RTMP协议的视频流直播。

通过Video渲染,并添加到VideoDisplay组件上显示。

<fx:Script>
  <![CDATA[
   
   private var connection:NetConnection;
   private var stream:NetStream;
   private var video:Video;
   
   private var serverAdd:String ;
   private var streamID:String ;
   
   protected function application_applicationCompleteHandler(event:FlexEvent):void
   {
    InitConnection();
   }
   
   /**
    * 初始化连接
    */
   protected function InitConnection():void
   {
    connection = new NetConnection();
    connection.objectEncoding =  ObjectEncoding.AMF0;  //Compatible with low version
    connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
    video = new Video(videoDisplay.width,videoDisplay.height);   //TO DO IT during the initializaton, not definition
    video.smoothing = true; //do smooting operation during video stream zooming
   }
   
   /**
    * 连接RTMP服务器
    */
   protected function btnConnect_clickHandler(event:MouseEvent):void
   {
    if(textInputServerAdd.text.length == 0 || textInputStreamID.text.length == 0)
    {
     return ;
    }
    serverAdd = textInputServerAdd.text ;
    streamID  = textInputStreamID.text ;
    
    connection.connect(serverAdd);
   }
   
   protected function netStatusHandler(event:NetStatusEvent):void
   {
    trace("code:"+event.info.code+" "+"level:"+event.info.level+" "+"description:"+event.info.description);
    switch (event.info.code)
    {
     case "NetConnection.Connect.Success":
      beginPlay(); //start to play after connecting successfully
      break;
     
     case "NetConnection.Connect.Failed":
      break;
     
     case "NetConnection.Connect.Closed":
      break;
     
     case "NetStream.Play.Start":
      break;
     
     case "NetStream.Play.StreamNotFound":
      break;
     
     case "NetStream.Buffer.Full":
      break;
     
     default:
      return ;
    }
   }
   
   /**
    * 服务器连接成功之后,直接开始播放视频
    */
   protected function beginPlay():void
   {
    var client:Object= new Object();
    
    stream = new NetStream(connection);
    
    stream.addEventListener(NetStatusEvent.NET_STATUS , netStatusHandler);
    
    stream.client = client;
    client.onMetaData = onMetaDataHandler;
    
    video.attachNetStream(stream);
    stream.play(streamID);
    videoDisplay.addChild(video);
   }
   
   /**
    * 视频元数据(onMetaData事件)
    */
   protected function onMetaDataHandler(info:Object):void
   {
    /** 视频元信息 **/
    for (var propName:String in info)
    {
     trace(propName + " = " + info[propName]);
    }
   }
   
  ]]>
 </fx:Script>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值