action script, flash, ns - loop a flv if a condition is true

702 views Asked by At

Good day to all.

I need to loop a flv file in flash if a condition is true.

So I tryed to add a listener but id doesn't work.. Code below:

textBox.addEventListener(FocusEvent.FOCUS_IN, focusInListener);

var connection:NetConnection = new NetConnection(); 
var stream:NetStream; 
var video:
Video = new Video(200, 200); 
var metaObj:Object = new Object();

function onMetaData(data:Object):void {

}

var checkvid=0;

connection.connect(null); 
stream = new NetStream(connection); 
stream.client = metaObj; 
metaObj.onMetaData = onMetaData; 
video.attachNetStream(stream); 
addChild(video); 
stream.play("Mann ft. 50 Cent - Buzzin 2010-(mrsjs).flv"); 
video.x = 0; 
video.y = 40;

textBox.addEventListener(KeyboardEvent.KEY_DOWN,handler);

function handler(event:KeyboardEvent)
{     
   if(event.charCode == 13) // if the key is ENTER
   {
      switch (textBox.text)
     {
       case "1": 
               stream.play("m1.flv");  
               checkvid=0;
               // Note from Slomojo - You probably wanted a break; here.
           case "2": 
               stream.play("m2.flv");
               checkvid=1; 
               break;
           case "3": 
               stream.play("m3.flv");
               checkvid=0; 
               break;
     }
    }
}

var listener:Object = new Object();
listener.complete = function():Void {
   stream.seek(0);
   stream.play();
}
stream.addEventListener("complete", listener);

Any idea is appreciated. Thank you.

2

There are 2 answers

2
ocodo On BEST ANSWER

NetStream doesn't fire a complete event, and you're doing the listener object incorrectly, you use a direct callback function for the NetStream

See here: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/net/NetStream.html#event:onPlayStatus

Basically, you listen to onStatus - assign a callback to it... ie:

stream_ns.onStatus = function(infoObject:Object) {
        trace("NetStream.onStatus called: ("+getTimer()+" ms)");
        for (var prop in infoObject) {
            trace("\t"+prop+":\t"+infoObject[prop]);
        }
};

The information you want is in the infoObject.

0
theaibo On

I suggest this solution:

    var stream_ns:NetStream ...
    stream_ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
    ...
    stream_ns.play("video.flv");

    function netStatusHandler(p_evt:NetStatusEvent):void {
        if (p_evt.info.code == "NetStream.FileStructureInvalid") {
            trace("The MP4's file structure is invalid.");
        }
        else if (p_evt.info.code == "NetStream.NoSupportedTrackFound") {
            trace("The MP4 doesn't contain any supported tracks");
        }
        else if (p_evt.info.code == "NetStream.Play.Stop") {
            trace("final of video");
            repeatMethod();
        }
    }

So, you need to implement the repeadMethod, this can be as:

    function repeatMethod():void {
          stream_ns.play("video.flv");
    }