9 Replies Latest reply: May 4, 2013 2:09 AM by vikashsinghal RSS

    SWFLoader : stop/ pause / play features ?

    Celinio

      hi,

      I am using the SWFLoader component to play Flash files (with SWF extension).

       

      <mx:SWFLoader id="animation_toto" x="24" y="36" source= "toto.swf" autoLoad="true" width="657" height="164"/>

       

      I am able to control the start of the animation with the load() method.

       

      animation_toto.load();

       

      However, I do not see stop / pause methods to stop / pause the animation.

      (But they exist in the VideoDisplay component.)

       

      How do i do that ? Is there a way ?

       

      Thanks a lot for helping.

        • 1. Re: SWFLoader : stop/ pause / play features ?
          CoreyRLucier Adobe Employee

          Have you tried this (once SWF is loaded)...

           

          if (animation_toto.content is MovieClip)

          {

              var foo:MovieClip = animation_toto.content;

              foo.stop();

          }

          • 2. Re: SWFLoader : stop/ pause / play features ?
            Celinio Community Member

            thanks but it does not compile. Does anyone have another suggestion ?

            • 3. Re: SWFLoader : stop/ pause / play features ?
              CoreyRLucier Adobe Employee

              What is the compile error?

               

              If it's possible to post a small example SWF (e.g. toto.swf) it would be helpful.

              • 4. Re: SWFLoader : stop/ pause / play features ?
                Zen Seven Community Member

                Hi there,

                 

                I'm having the exact same issue. I solved the compiler error by importing MovieClip and casting as such.

                 

                import mx.controls.SWFLoader;

                 

                private function Swf_Loaded(e:Event):void
                {
                            var newClip:MovieClip = loader.content as MovieClip;
                           
                            newClip.stop();
                }

                 

                HOWEVER this did not stop the swf from playing. It just plays and loops forever. Anyone have any idea on how to make this work? Why does it auto play when it loads in the first place? Adobe?

                • 5. Re: SWFLoader : stop/ pause / play features ?
                  Zen Seven Community Member

                  I have figured it out!

                   

                  For some insane reason calling stop() or play() on a parent movieclip does not stop() or play() the contents of that movie clip!! So.. to be able to stop everything, you need a recursive function, like so:

                   

                  // Our own stop function because flash is retarded
                  private function RecursiveStop(mc:MovieClip):void
                  {
                      for(var i:int = 0; i < mc.numChildren; i++)
                      {
                          var obj:Object = mc.getChildAt(i);
                         
                          trace(obj); // debugging
                         
                          if(mc.getChildAt(i) is MovieClip || getQualifiedSuperclassName(obj) == "flash.display::MovieClip")
                          {
                              RecursiveStop(obj as MovieClip);
                          }
                      }
                      mc.stop();
                  }

                   

                  Just looking for MovieClips wasn't good enough because in Flash you can embed objects as classes and they take on their own custom names. That's why

                  getQualifiedSuperclassName

                   

                  is necessary. To reset the movieclip back to frame and replay, use mc.gotoAndPlay(0). I'll be definitely creating my own class to wrap my loaded movie clip with for ease of use.

                   

                  This code has been confirmed working with Flex SDK 3.3, with both SwfLoader and Loader load code.

                   

                  Note: if you have a sound embeeded in your swf file it won't stop when you hit stop. However, if you run this function inside your event.COMPLETE handler it won't start playing it in the first place. Sounds like a bug. No pun intended.   

                   

                  ZS

                  • 6. Re: SWFLoader : stop/ pause / play features ?
                    mahesh.extentia

                    Hello Zen,

                     

                    Thanks for the RecursiveStop() function tip. I tried this out in my Flex application where I have to play one movie clip after another but without success. Here is the scenario: I am using a SWFLoader class on the main application form and have set a swf path. On application initialization, I call the stop() method to the MovieClip that is loaded in my SWFLoader object.

                     

                    MovieClip(Application.application.mySwfLoader.content).stop(); // THIS WORKS

                     

                    Later on click of a button, I dynamically store the paths of the swf files that I want to play one after another into an ArrayCollection and call a function to play the first item in the ArrayCollection.

                     

                    myArrayCollection.addItem("assets/beginning_swfs/0114INSTR.swf");

                    myArrayCollection.addItem("assets/beginning_swfs/0111INSTR.swf");

                    myArrayCollection.addItem("assets/beginning_swfs/0201INSTR.swf");

                    myArrayCollection.addItem("assets/beginning_swfs/0202INSTR.swf");

                     

                    setTimeout(PlayMovieClip, 1000);

                     

                    In the PlayMovieClip() function, I set the path of the first item (swf file) as the source of the SWFLoader object in my application, again assign the content of the SWFLoader object to a MovieClip object and add an eventlistener - Event.ENTER_FRAME along with the method checkFrame() that will be invoked when the EVENT_FRAME event is raised. After this I call the MovieClip.play() function.

                     

                    if (myArrayCollection.length == 0)

                    {

                    return;

                    }


                    path=String(myArrayCollection.getItemAt(0));

                     

                    trace("Starting movie: " + String(myArrayCollection.getItemAt(0)));

                     

                     

                    Application.application.mySwfLoader.source = path;

                     

                     

                    flashMovie=Application.application.mySwfLoader.content as MovieClip

                    flashMovie.addEventListener(Event.ENTER_FRAME, checkFrame);

                    flashMovie.play();

                     

                    Now once in the checkFrame() method, I determine if the end of the MovieClip has been reached. Stop the movie if the end frame has been reached.

                     

                    if (event.target.currentFrame == event.target.totalFrames)

                    {

                         trace("Stopping movie: " + String(myArrayCollection.getItemAt(0)));

                        

                         // stop the movie, call the RecursiveStop function.

                         RecursiveStop(MovieClip(event.target)); //event.target cast to MovieClip

                         event.target.removeEventListener(Event.ENTER_FRAME, checkFrame);


                         // Check if the array has elements in it.

                         if (myArrayCollection.length != 0)

                         {

                              // Remove the top most one.

                              myArrayCollection.removeItemAt(0);

                         }


                         // Check if there are more items in the array.

                         if (myArrayCollection.length != 0)

                         {

                              // Play the next file

                              PlayMovieClip();

                         }

                    }

                     

                    I expect the MovieClip files (SWF) to play one after another but there is an overlap and the movies do not stop playing.

                     

                    Any help pointing what I am doing wrong or a push in the right direction is highly appreciated. I've spent at least 3 days now trying to solve this issue without success.

                    • 7. Re: SWFLoader : stop/ pause / play features ?
                      agus6287

                      Hi,

                      I have a problem with

                        var newClip:MovieClip = loader.content as MovieClip;

                                 

                                  newClip.stop();

                      }

                       

                        newClip.stop() gives me a null pointer exception becuase the association of  var newClip:MovieClip = loader.content as MovieClip; gives me null. What could it be?

                      The video loads, the thing is when i transform it into movie clip.

                       

                      can you help me please?

                       

                      agustina

                      • 8. Re: SWFLoader : stop/ pause / play features ?
                        4xyfenix Community Member

                        Recursive play/stop works fine if applied to proper object.

                         

                        This works for me.

                         

                              //-----------------------------------------------------------------------

                              public function play_background(play: Boolean, hide: Boolean = true): void

                              {

                                try

                                {

                                  contentGroup.visible = play || !hide;

                                  var bg: SWFLoader = SWFLoader(contentGroup.getElementAt(0));

                         

                         

                                  if (bg.content is MovieClip)

                                  {

                                    var root: MovieClip = bg.content as MovieClip;

                                   

                                    if (root.numChildren != 0 && root.getChildAt(0) is Loader)

                                    {

                                      var loader: Loader = root.getChildAt(0) as Loader;

                                     

                                      if (loader.content is MovieClip)

                                      {

                                        play_clip(MovieClip(loader.content), play);

                                      }

                                    }

                                  } else

                                  {

                                    trace("[mobile]: Background object is not movie clip");

                                  }

                                } catch (e: Error)

                                {

                                  trace("[mobile]: Play background error:", e);

                                }

                              }

                         

                         

                              //-----------------------------------------------------------------------

                              private function play_clip(clip: MovieClip, play: Boolean): void

                              {

                                if (play) { clip.play(); } else { clip.stop(); }

                               

                                for (var i: int = 0; i < clip.numChildren; ++i)

                                {

                                  var obj: DisplayObject = clip.getChildAt(i);

                                  if (obj is MovieClip)

                                  {

                                    play_clip(obj as MovieClip, play);

                                  }

                                }

                              }

                        • 9. Re: SWFLoader : stop/ pause / play features ?
                          vikashsinghal

                          swfLoader.unloadAndStop(true);

                           

                          Works for this issue . The Flex Documentation says :

                          Unloads an image or SWF file. After this method returns the source property will be null. This is only supported if the host Flash Player is version 10 or greater. If the host Flash Player is less than version 10, then this method will unload the content the same way as if source was set to null. This method attempts to unload SWF files by removing references to EventDispatcher, NetConnection, Timer, Sound, or Video objects of the child SWF file. As a result, the following occurs for the child SWF file and the child SWF file's display list:
                          • Sounds are stopped.
                          • Stage event listeners are removed.
                          • Event listeners for enterFrame, frameConstructed, exitFrame, activate and deactivate are removed.
                          • Timers are stopped.
                          • Camera and Microphone instances are detached
                          • Movie clips are stopped.