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>



猜你喜欢

转载自blog.csdn.net/mike_modern/article/details/20791147