ReadByte from URLStream failed in ActionScript Flash Builder

106 Views Asked by At

I'm new to ActionScript and have some problem with getting bytes from URLStream.

I'm trying to connect to a URL, and get the bytes from this URL.

I created private var urs:URLStream; and urs.connected returns true. Now I want to get the byte from this url. My code is:

public class myClass extends MovieClip
{
    private var urs:URLStream;
    private var ns:NetStream;
    private var urr:URLRequest;
    private var textStr:TextField;

    public function myClass()
    {
        var urr:URLRequest;
        var urs:URLStream;
        urr=new URLRequest("http://myserver/video/video.flv");
        urs=new URLStream();
        urs.addEventListener(flash.events.ProgressEvent.PROGRESS,progresHnd);
        urs.addEventListener(flash.events.Event.COMPLETE,completeHnd);
        urs.addEventListener(flash.events.Event.CLOSE,closeHnd);
        urs.addEventListener(flash.events.Event.OPEN,openHnd);
        urs.load(urr);

        if (urs.connected)
        {
            var myint:int = urs.readByte();
            textStr.text = "success";
            addChild(textStr);
        }
        else 
        {
            textStr.text = "urs not connected";
            addChild(textStr);
        }
    }
}

The code failed at urs.readByte(); . (Maybe with error #2030, not sure about it)

Someone can help me with a solution for this? (or give another way to get the bytes)

Thanks!

1

There are 1 best solutions below

0
On BEST ANSWER

Connected means whether the server is connected or not, it does not mean that data loading is complete. To read bytes you need to wait until the data load is complete, i.e. you can read bytes in your complete handler.

public function completeHnd(evt:Event):void {
    var myint:int = urs.readByte();
}

Additional note, when you are adding var urs:URLStream; and var urs:URLStream; in constructor they are hiding class member urs and urs. So you need to remove these declarations from constructor.

// remove these two lines from constructor
var urs:URLStream;
var urr:URLRequest;