8 Replies Latest reply on Nov 29, 2012 10:22 AM by blairbooth1992

    Controling an external swf with play and stop button functionality

    blairbooth1992 Level 1

      I'm using flash as3 cs5.  All I want to do is: import an external swf into a new fla and add a button for play and stop functionality.  The swf is just a graphic eq with music for background and I wish the user to be able to stop the swf sound and graphics.  The button play_Btn stops all sound and graphics but won't restart.

       

      Here's my script thus far:

       

      var Xpos:Number = 1;

      var Ypos:Number = -20;

      var swf:MovieClip;

      var loader:Loader = new Loader();

       

       

      var defaultSWF:URLRequest = new URLRequest("swfs/eq.swf");

       

       

      loader.load(defaultSWF);

      loader.x = Xpos;

      loader.y = Ypos;

      addChild(loader);

      ////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////

       

       

      // Btns Universal function

      function btnClick(event:MouseEvent):void {

       

                removeChild(loader);

                var newSWFRequest:URLRequest = new URLRequest("swfs/" + event.target.name + ".swf");

                loader.load(newSWFRequest);

                loader.x = Xpos;

          loader.y = Ypos;

                addChild(loader);

      }

      // Btn listeners

      play_Btn.addEventListener(MouseEvent.CLICK, fl_ClickToStopAllSounds);

       

       

      function fl_ClickToStopAllSounds(event:MouseEvent):void

      {

                SoundMixer.stopAll();

      }

        • 1. Re: Controling an external swf with play and stop button functionality
          kglad Adobe Community Professional & MVP

          what is it that you need to stop in your loaded swf?  the main timeline playing?  some movieclip playing?  do you need to call a function in the loaded swf? something else?

          • 2. Re: Controling an external swf with play and stop button functionality
            blairbooth1992 Level 1

            Basically it's just an mp3 of background music with a lovely graphic eq which I converted to swf as I couldn't get any buttons to work on the original file.

             

            I'm trying to remove child but now with my buttons the background swf wont autoplay, which forces the user to hit a button at the start just to play back ground music:  here's what I have thus far:  Oh and I using a container for the swf!  Thanx for helping me!  I am new to this, but trying very hard!

             

            stop();

            import flash.events.MouseEvent;

            import flash.events.Event;

             

             

            var Xpos:Number = 1;

            var Ypos:Number = -20;

            var swf:MovieClip;

            var loader:Loader = new Loader();

             

             

            var defaultSWF:URLRequest = new URLRequest("swfs/eq.swf");

            on_mc.addEventListener(MouseEvent.CLICK,clickF);

             

             

             

             

            function clickF(event:MouseEvent):void{

                      loader.load(defaultSWF);

                      loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadNowF);

                      function loadNowF(event:Event):void{

                                _1.addChild(loader);

                      }

                      loader.x = Xpos;

                      loader.y = Ypos;

             

            }

            ////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////

             

             

            // Btns Universal function

            function btnClick(event:MouseEvent):void {

             

                      removeChild(loader);

                      var newSWFRequest:URLRequest = new URLRequest("swfs/" + event.target.name + ".swf");

                      loader.load(newSWFRequest);

                      loader.x = Xpos;

                loader.y = Ypos;

                      addChild(loader);

            }

            // Btn listeners

            off_mc.addEventListener(MouseEvent.CLICK, fl_ClickToStopAllSounds);

             

             

            function fl_ClickToStopAllSounds(event:MouseEvent):void

            {

                      SoundMixer.stopAll();

            }   

            • 3. Re: Controling an external swf with play and stop button functionality
              kglad Adobe Community Professional & MVP

              apply unloadAndStop() to your loader:

               

              loader.unloadAndStop();

              • 4. Re: Controling an external swf with play and stop button functionality
                blairbooth1992 Level 1

                I put the load.unloadAndStop();

                 

                In a number of places but still no auto play at start:  Here's the script:  Could it be, that because I'm using a container to load the file which needs a button to call the child .swf in is the problem?

                 

                Much thanks!

                 

                stop();

                import flash.events.MouseEvent;

                import flash.events.Event;

                 

                var Xpos:Number = 1;

                var Ypos:Number = -20;

                var swf:MovieClip;

                var loader:Loader = new Loader();

                 

                 

                var defaultSWF:URLRequest = new URLRequest("swfs/eq.swf");

                on_mc.addEventListener(MouseEvent.CLICK,clickF);

                 

                 

                function clickF(event:MouseEvent):void{

                    loader.load(defaultSWF);

                    loader.unloadAndStop();

                    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadNowF);

                    function loadNowF(event:Event):void{

                        _1.addChild(loader);

                       

                    }

                    loader.x = Xpos;

                    loader.y = Ypos;

                   

                }

                ////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////

                 

                // Btns Universal function

                function btnClick(event:MouseEvent):void {

                   

                    removeChild(loader);

                    var newSWFRequest:URLRequest = new URLRequest("swfs/" + event.target.name + ".swf");

                    loader.load(newSWFRequest);

                    loader.x = Xpos;

                    loader.y = Ypos;

                    addChild(loader);

                   

                }

                // Btn listeners

                off_mc.addEventListener(MouseEvent.CLICK, fl_ClickToStopAllSounds);

                 

                function fl_ClickToStopAllSounds(event:MouseEvent):void

                {

                    SoundMixer.stopAll();

                }   

                • 5. Re: Controling an external swf with play and stop button functionality
                  kglad Adobe Community Professional & MVP

                  what, in the loaded swf, is supposed to start?  i know you want sound and animation to start but that is not enough information.  you need (and anyone trying to help you needs) to know what causes the animation and sound to start.

                  • 6. Re: Controling an external swf with play and stop button functionality
                    blairbooth1992 Level 1

                    The loaded swf contains only 1 mp3 and graphic equalizer that animates to the music.  The Main script that created the flash is listed below:

                     

                    package {

                        import flash.media.*;

                        import flash.net.*;

                        import flash.display.*;

                        import flash.events.*;

                       

                        import com.everydayflash.equalizer.*;

                        import com.everydayflash.equalizer.color.*;

                       

                        public class Main extends Sprite{

                            public function Main() {

                                var s:Sound = new Sound(new URLRequest("track.mp3"));

                                s.play(0, 100, new SoundTransform(1, 0));

                               

                                var es:EqualizerSettings = new EqualizerSettings();

                                es.numOfBars = 32;

                                es.height = 64;

                                es.barSize = 4;

                                es.vgrid = true;

                                es.hgrid = 2;

                                es.colorManager = new SolidBarColor(0xffff4444);

                                es.effect = EqualizerSettings.FX_REFLECTION;

                               

                                var e:Equalizer = new Equalizer();

                                e.update(es);

                                e.x = 0;

                                e.y = -20;

                                addChild(e);

                               

                                addEventListener(Event.ENTER_FRAME, e.render);

                               

                           

                            }

                        }

                    }

                    • 7. Re: Controling an external swf with play and stop button functionality
                      kglad Adobe Community Professional & MVP

                      if you can edit that code, change it to:

                       

                       

                       

                      package {

                          import flash.media.*;

                          import flash.net.*;

                          import flash.display.*;

                          import flash.events.*;

                       

                          import com.everydayflash.equalizer.*;

                          import com.everydayflash.equalizer.color.*;

                       

                          public class Main extends Sprite{

                       

                      public var s:Sound;

                      public var e:Equalizer;

                      public var sc:SoundChannel;


                              public function Main() {

                                 s = new Sound(new URLRequest("track.mp3"));

                                  sc = s.play(0, 100, new SoundTransform(1, 0));

                       

                                  var es:EqualizerSettings = new EqualizerSettings();

                                  es.numOfBars = 32;

                                  es.height = 64;

                                  es.barSize = 4;

                                  es.vgrid = true;

                                  es.hgrid = 2;

                                  es.colorManager = new SolidBarColor(0xffff4444);

                                  es.effect = EqualizerSettings.FX_REFLECTION;

                       

                                  e = new Equalizer();

                                  e.update(es);

                                  e.x = 0;

                                  e.y = -20;

                                  addChild(e);

                       

                                  addEventListener(Event.ENTER_FRAME, e.render);

                       

                       

                              }

                          }

                      }

                      • 8. Re: Controling an external swf with play and stop button functionality
                        blairbooth1992 Level 1

                        Ok I popped in your code on the Main.as and exported a new swf which plays fine, but I can't seem to get my button to unload the swf:  here's the code.  Basically I just want a play/pause button to start and stop the swf.

                         

                        Kind and generous thank yous!

                         

                        var Xpos:Number = 0;

                        var Ypos:Number = 0;

                        var swf:MovieClip;

                        var loader:Loader = new Loader();

                         

                        var defaultSWF:URLRequest = new URLRequest("example.swf");

                         

                        loader.load(defaultSWF);

                        loader.unloadAndStop();

                        loader.x = Xpos;

                        loader.y = Ypos;

                        addChild(loader);

                         

                        play_Btn.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF_2);

                         

                        var fl_Loader_2:Loader;

                         

                        //This variable keeps track of whether you want to load or unload the SWF

                        var fl_ToLoad_2:Boolean = true;

                         

                        function fl_ClickToLoadUnloadSWF_2(event:MouseEvent):void

                        {

                            if(fl_ToLoad_2)

                            {

                                fl_Loader_2 = new Loader();

                                fl_Loader_2.load(new URLRequest("swfs/green.swf"));

                                addChild(fl_Loader_2);

                            }

                            else

                            {

                                fl_Loader_2.unload();

                                removeChild(fl_Loader_2);

                                fl_Loader_2 = null;

                            }

                            // Toggle whether you want to load or unload the SWF

                            fl_ToLoad_2 = !fl_ToLoad_2;

                        }