actionscript-3flashrtmpadobe-media-server

How to run a method when onPublish is triggered in Adobe Media Server?


I have the following code inside Adobe Media Server's main.asc (latest version, 5.0.10 I think):

application.onPublish = function (clientObj, streamObj) {
  for (var i = 0; i < application.clients.length; i++){
    application.clients[i].call("streamConnected");
  }
}

And this code inside my ActionScript (3.0) file, connected to my flash file:

nc = new NetConnection();

nc.addEventListener(NetStatusEvent.NET_STATUS, onConnectionStatus);
nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
nc.client = { onBWDone: function():void{}, streamConnected: function():void{} };
nc.connect(videoURL);

...

public function streamConnected(...rest):void {
  trace("Stream Connected");
}

I'm not too sure what my code means - most of it has been sourced from various sections of the Internet, so any help would be greatly appreciated.


Solution

  • Using your current code, the only function that will be executed is the empty one which is defined inside the nc.client object because the streamConnected() function is not attached to the nc.client's streamConnected property.

    So, to get the "Stream Connected" message, you can change that anonymous function like this for example :

    nc.client = { 
        onBWDone: function():void{}, 
        streamConnected: function(...rest):void {
            trace("Stream Connected");
        }
    };
    

    or simply you can use your existing streamConnected() function :

    nc.client = { 
        onBWDone: function():void{}, 
        streamConnected: streamConnected
    };
    

    Hope that can help.