3 Replies Latest reply on Mar 25, 2008 7:36 AM by anthonws

    FLVPlayback load 50% then play

    anthonws
      Hello to all,

      My 1st post here!

      I'm trying to publish a video in a way that it would only play after 50% is loaded.

      I'm using CS3 with AS 2.0.

      Here is the code (that I found online and adapted):
      -----------------------------------------------------------------------------------
      import mx.video.*;
      var listenerObject:Object = new Object();

      listenerObject.ready = function(eventObject:Object):Void {
      my_FLVPlybk.pause();
      trace("State is " + eventObject.state);
      // display the no. of bytes loaded at this point
      trace("Bytes loaded: " + my_FLVPlybk.bytesLoaded + " Bytes Total / 2: " + (my_FLVPlybk.bytesTotal / 2));
      while(my_FLVPlybk.bytesLoaded < (my_FLVPlybk.bytesTotal / 2)){
      }
      trace("Loaded!");
      my_FLVPlybk.play();
      };
      my_FLVPlybk.contentPath = "teste.flv";
      my_FLVPlybk.addEventListener("ready", listenerObject);

      -----------------------------------------------------------------------------------
      There is no error when I test locally, and I get the right outputs, but with the online test my browser tells me that the script is taking to long to bla bla bla...

      I'm new to AS so be gentle! :D

      Thank you all!

      Anthonws.
        • 1. Re: FLVPlayback load 50% then play
          anthonws Level 1
          Well,

          I found some errors and changed it.

          Still no go...
          ------------------------------------------------------------------------
          import mx.video.*;
          var listenerObject:Object = new Object();

          listenerObject.ready = function(eventObject:Object):Void {
          // Pauses the Movie
          my_FLVPlybk.pause();
          //while less than the half of the movie size (Total = 14,3 Mb, Half = 7,15 Mb [round values])
          while(my_FLVPlybk.bytesLoaded < 7638381){
          trace("Bytes loaded: " + my_FLVPlybk.bytesLoaded);
          }
          // If loaded output message and start playing the Movie
          trace("Loaded!");
          my_FLVPlybk.play();
          };
          // Starts the movie
          my_FLVPlybk.contentPath = "../teste.flv";
          // Run the listener function
          my_FLVPlybk.addEventListener("ready", listenerObject);
          ------------------------------------------------------------------------
          • 2. Re: FLVPlayback load 50% then play
            Greg Dove Level 4
            I think the biggest issue here is that you're aiming to use a while loop to 'pause' your code. That's not the way to do it, as the loop still executes and will trigger the script timeout error.
            You should remove that loop, and also remove the my_FLVPlybk.play(); method call from your 'ready' event listener function. You don't need the ready event at all I think. You can use my_FLVPlybk.autoPlay=false; to prevent the component autoplaying the content, instead of setting pause in the ready state.

            The general way you delay things in flash is either via a event listener that triggers after something has happened or by checking things in a time (setInterval) or frame based loop. Fortunately the FLVPlayback component has another event that will help you out here and hopefully it will help you here.

            Add an event listener for the progress event. This will be called at a frequency determined by the progressInterval property of the FLVPlayback instance. In that listener function check bytesLoaded vs bytesTotal, and then if its above the 50% mark:
            a) remove the event listener for the progress event - it is no longer needed &
            b) call my_FLVPlybk.play();

            Here's the reference to the progress event:
            http://livedocs.adobe.com/flash/9.0/main/00003045.html#wp3726457
            • 3. Re: FLVPlayback load 50% then play
              anthonws Level 1
              Thank you so much for your answer!

              I got the same kind of reply from kirupa forum, along with the code :)

              Here is my final code:

              my_FLVPlybk.addEventListener("progress", loadingAdvanced);
              my_FLVPlybk.width = 480;
              my_FLVPlybk.height = 200;
              my_FLVPlybk.load("../teste.flv");

              function loadingAdvanced(e) : Void {
              var percent : Number = Math.round((e.bytesLoaded/e.bytesTotal)*100);
              if(percent > 50) {
              my_FLVPlybk.removeEventListener("progress", loadingAdvanced);
              // MovieClip Invisivel
              if(loading._visible == true){
              loading._visible = false;
              }
              my_FLVPlybk.play();
              }

              I cannot use my C way of thinking here :P

              Best Regards,
              Anthonws.