控制FLVPlayback的NetConnection

来源:互联网 发布:周杰伦的才华 知乎 编辑:程序博客网 时间:2024/05/22 03:07

很不幸,我把原文的地址给忘掉了。这是国外一个论坛上的帖子,希望对大家有帮助。

Accessing the NetConnection Object in FLVPlayback

Posted At : August 14, 2009 9:13 AM | Posted By : Stefan Richter | Related Categories: Components , FMS , Videos & Players

 

 

There are situations where it is necessary to call a server side method on FMS. Some CDNs, for example Limelight, used to or maybe still do require you to call the FCSubscribe method in order to request a live stream. This send a signals to the Edge server to pull the live stream from the Origin server if it is not already being delivered to that Edge. While this delivery method and stream setup routine is being phased out across most CDNs I thought it may be useful to post a (slightly hackish) workaround to make this setup work with the FLVPlayback component.

 

The problem with the FLVPlayback component is that there is no obvious, simple way to obtain a reference to the NetConnection Object it uses under the hood. Sure, the ncMgr.getNetConnection let's you grab it but only once the connection is established, and while you can implement a custom NCManager class, this is not trivial and after all a NetConnection is being maintained already by the component, so why reinvent the wheel?

The following code is clearly not something I am proud of, but it worked at the time when I needed it. It was used to get a live stream working with the FLVPlayback component streaming from Limelight about a year or two ago.

 

代码:

// listen to player events and kill manual connection once we're streaming
   player.addEventListener(
"playing", playListener);
   player.addEventListener(
"stateChange", stateListener);
   player.addEventListener(
"ready", readyListener);
      
/* this is the hack: check once every frame if the NC has been defined inside the FLVPlayback component */   
   this.onEnterFrame = function()
   {
      if (player.ncMgr.getNetConnection() != undefined)
      {
         this.onEnterFrame = null;
         delete this.onEnterFrame;
         trace(
"got NC");
      
   //subscribe(streamName);       }
   }
         
   var nc:NetConnection;
   var serverName:String =
"server.llnwd.net";
   var appName:String =
"account_name/_definst_";
   var streamName:String =
"live";
   
   var source_Str =
"rtmp://" + serverName + "/" + appName + "/" + streamName;
   
   // start up by setting the contentPath (now called source in newer versions of the component)    
player.contentPath = source_Str;   
   
   function subscribe(name:String)
   {
      nc = player.ncMgr.getNetConnection();
         
      nc.onFCSubscribe = function(info:Object)
      {
         trace(
"onFCSubscribe: " + info.code);
         clearInterval(int_id);

         if (info.code ==
"NetStream.Play.StreamNotFound")
         {
         
   // handle error, retry after a few secs or similar          }
         else if (info.code ==
"NetStream.Play.Start")
         {
         
   // we're successfully subscribed          }
         else
         {
         
   // handle error          }
      };
         
      
// not used right now       
nc.onFCUnsubscribe = function(info:Object)
      {
      }
         
      trace(
"subscribing to " + name);
      nc.call(
"FCSubscribe",null,name);
   }
   
   // can be used to unsubscribe from stream    
function unsubscribe(name:String)
   {
      nc.call(
"FCUnsubscribe",null,name);
   }

 

 

 

Hopefully this is helpful to someone.

 

 

原创粉丝点击