1

I'm really not familiar with Action Script 3 at all but I am with other languages.

I'm hoping someone could help me.

I'm attempting to make a modification to JWplayer so that an rtmp stream is retrieved via a PHP script rather than it being supplied in the HTML.

The code I currently have is below:

    function useData(event:Event):void {
            var data:String = event.target.data.toString();

        }

        /** Load content. **/
        override public function load(itm:PlaylistItem):void {
            _item = itm;
            _position = 0;

            var loader:URLLoader = new URLLoader();
            loader.addEventListener(Event.COMPLETE, useData);
            loader.load(new URLRequest("http://192.168.0.12/phpauth/play1.php"));

            // Set Video or StageVideo
if(!_video) { 
                _video = new Video(320, 240);
                _video.smoothing = true;
                _video.addEventListener('renderState', renderHandler);
                // Use stageVideo when available
                if (_stageEnabled && RootReference.stage['stageVideos'].length > 0) {
                    _stage = RootReference.stage['stageVideos'][0];
                    _stage.viewPort = new Rectangle(0,0,320,240);
                    _stage.addEventListener('renderState', renderHandler);
                }
                attachNetStream(_stream);
            }
            // Load either file, streamer or manifest
            if (_item.file.substr(0,4) == 'rtmp') {
                // Split application and stream
                var definst:Number = _item.file.indexOf('_definst_');

In the load function the file name to play is held in _item.file. I'm trying to make a call to a php script which then overwrites the value in _item.file. I've confirmed that the php is being called but I don't know how to get the data from the data string in the useData function into the _item.file string.

Any help would be really appreciated - I suspect this is a simple one but my lack of AS3 knowledge is making it really difficult.

Thanks,

1
  • hey, did the answer below help your case work out?? If it did, I think it's worth to update the question to specify the actual issue so that it can be useful to someone else hitting it.... Commented Sep 13, 2013 at 10:57

2 Answers 2

1

Your problem basically about how to access a local variable in an event handler. A quick and dirty way can be to have an anonymous function used as a handler like:

loader.addEventListener(Event.COMPLETE, function(event:Event):void {
    var data:String = event.target.data.toString();
    _item.file = data;
});

This approach would work, because this anonymous function has access to the local variables inside load function as is. But, you need to be cautious that the anonymous function uses the variable exactly as the calling function is using. So, let's say there is a loop in load function and _item changes in every iteration of the loop. For that scenario, when load handler gets called, its _item would also have changed to the object which was last assigned to _item.

A far cleaner and OO approach can be to have a handler class like:

package {
public class LoadHandler {
    private var _item:PlaylistItem;

    public function LoadHandler(item:PlaylistItem) {
        _item = item;
    }

    public function loadHandler(event:Event):void {
        var data:String = event.target.data.toString();
        _item.file = data;
    }
}

and then have loader.addEventListener(Event.COMPLETE, (new LoadHandler(_item)).loadHandler). Hope that helps. BTW, LoadHandler could be made more generic to take and array of objects to be used and a callback function. loadHandler function, then could just call callback function with that array of objects.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, really appreciate your help on this. I've tried your first code snippet but it seems the overall result is the same. I think _item.file is being assigned as a local variable within the event handler function so it has no effect on the variable I want to change within the calling load() function. I think I need to have some sort of get data function or something.
I checked your code after your comment to my answer and I think _item.file is empty string when you check for substr(,)=='rtmp'. That is because AS3 is event-drive. FP won't start loading play1.php until your load() function is done executing. So, you'd have to move rest of your code inside event handler.
1

If you are returning a simple string from PHP you should be able to use

event.target.data;

e.g. from PHP... echo "hello";

var data:String = event.target.data

You could try tracing the response to ensure you are getting something back from PHP. You can either test this from within the IDE or install the Debug version of the Flash Player browser plugin.

trace("Response from PHP: "+event.target.data);

_item.file = event.target.data;
trace("_item.file: "+_item.file);

1 Comment

Thanks, it's definetely returning the data from the PHP, the problem is I don't know how to assign the string to the _item.file string from the data variable in the usedata event handler function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.