13 Replies Latest reply: Jun 28, 2012 5:12 PM by kglad RSS

    Actionscript external interface onload

    Tony404 Community Member

      Hi

       

      I Have this external interface code that sends a video url to a video player when a user clicks a button it goes some thing like this:

       

      <script language="JavaScript" type="text/javascript">
      $(document).ready(function() {
      function callJavascript(sendText){

       

         window.document.myMovie.SetVariable("testValue", sendText);
      }

        });
      </script>

       

      The code above works fine when a user click a button with onclick the passed variable is a video url

      and is send to the player.

       

      The problem is I would like to get the video url passed when the page is loaded.

       

      I have tried.

       

      <script language="JavaScript" type="text/javascript">

      $(document).ready(function() {

       

      var sendText=$("Videosroll/Videos/1.flv").val();                                                                      

      window.document.myMovie.SetVariable("testValue", sendText);

       

      }

      </script>

      This does not work. I think its because the videoplayer is not fully loaded when this action is called. 

       

      I have looked at javascript timers etc. If any one has a solution please help.

        • 1. Re: Actionscript external interface onload
          kglad CommunityMVP

          use the externalinterface call() method to call callJavascript() (but it wouldn't make any sense to have flash pass sendText so that should be variable in callJavascript() )

          • 2. Re: Actionscript external interface onload
            Tony404 Community Member

            Hi

             

            Thanks for the reply. I am not sure what you mean. But on the callJavascript()

             

            <input type="button" onclick="callJavascript(videourl)" value="click me"/>

             

            is used and passes the videourl to play. Which works fine.

             

            The second part is an attempt to do the same but when the page is loaded. This is where

            the problem lay.

             

            thank you.

            • 3. Re: Actionscript external interface onload
              kglad CommunityMVP

              again,  in your html use:

               

              <script language="JavaScript" type="text/javascript">

               

              function callJavascript(){

                return videourl;  // <- make sure this is defined.  if it's not defined when your page loads, use a loop in flash to keep calling until it is defined.
              }


              </script>

               

              // in flash, use:

               

              import flash.Exteneral.ExternalInterface;

               

              var videoURL:String=ExternalInterface.call("callJavascript");

              // do whatever with videoURL;

              • 4. Re: Actionscript external interface onload
                Tony404 Community Member

                Hi

                 

                This is extremely helpful. My current actionscript is:

                 

                var testValue:String = "";

                watchCallback = function (id, oldval, newval):String {       

                    result_txt.text = newval;

                    myVid.play(newval);

                    return newval;   

                };


                _root.watch("testValue", watchCallback);

                 

                HTML/PHP:

                <?Php

                //This is a while loop with many videourls and thumb images. When clicked the javascript passes the video url to flash.

                 

                <a href="" onclick="return false" class="gt" name="'.$vidfield.'"><img src="'.$vidthumb.'" width="180" height="105" align=center /></a>

                 

                ?>

                 

                Current javascript:

                 

                <script language="JavaScript" type="text/javascript">
                $(document).ready(function() {

                    $(".gt").live("click", function(){

                 

                        var sendText = $(this).attr("name");

                        window.document.myMovie.SetVariable("testValue", sendText);

                 

                    });

                 

                  });
                </script>

                 

                The attempt is to automaticly send the a video url onload. So using your advice I added this to current code:

                 

                HTML:

                 

                <body onload="callnow(videourl)"></body>

                 

                 

                Javascript:

                 

                <script language="JavaScript" type="text/javascript">

                $(document).ready(function() {

                 

                function callnow(videourl){

                 

                return videourl;

                }

                   });

                </script>

                 

                Actionscript 2.0:

                 

                import flash.Exteneral.ExternalInterface;

                 

                var videoURL:String=ExternalInterface.call("callnow");

                 

                //Videoplay called myVid.

                myVid.play(videoURL);

                 

                This has failed to play the video url onload. Other effort include changing callnow function to:

                 

                function callnow(videourl){

                  window.document.myMovie.SetVariable("videoURL", videourl);

                return videourl;

                }

                 

                This has not been a success. I hope you can help and thank you again.

                • 5. Re: Actionscript external interface onload
                  kglad CommunityMVP

                  where is videourl (in your html document) defined?

                  • 6. Re: Actionscript external interface onload
                    Tony404 Community Member

                    Hi

                     

                    Its onload:

                    <body onload="callnow(Videos/1.flv)"></body>

                     

                    Thanks.

                    • 7. Re: Actionscript external interface onload
                      kglad CommunityMVP

                      use:

                       

                      <script language="JavaScript" type="text/javascript">

                       

                      function callJavascript(){

                        return "Videos/1.flv";
                      }


                      </script>

                      • 8. Re: Actionscript external interface onload
                        Tony404 Community Member

                        Hi

                         

                        Just gave it a try. mite be a syntax error.

                         

                        HTML:

                        <body onload="callnow()"></body>

                         

                        JAVASCRIPT:

                        <script language="JavaScript" type="text/javascript">

                        $(document).ready(function() {

                         

                        function callnow(){

                         

                        return "Videosroll/Videos/1.flv";

                        }

                           });

                        </script>

                         

                        ACTIONSCRIPT:

                        import flash.Exteneral.ExternalInterface;

                        var videoURL:String=ExternalInterface.call("callnow");

                         

                        //Videoplay called myVid.

                         

                        myVid.play(videoURL);

                         

                        I must say I am using actionscript 2.0. also. On callnow in actionscript should they be a () at the end. Like callnow() etc.

                         

                        Thank you.

                        • 9. Re: Actionscript external interface onload
                          kglad CommunityMVP

                          i have no idea why you keep changing the javascript code i suggest.  the only javascript needed is in message 7.  that's it.

                           

                          if that fails, add a textfield, assign it an instance name (eg, tf) and use the following actionscript:

                           

                          import flash.Exteneral.ExternalInterface;

                          var videoURL:String=ExternalInterface.call("callnow");

                           

                          tf.text=videoURL;

                           

                          myVid.play(videoURL);

                          • 10. Re: Actionscript external interface onload
                            Tony404 Community Member

                            Hi Kglad

                             

                            Thank you so much for the assistance. Sorry for the javascript changes I made I had turned the function into callnow.

                             

                            I falled message 7.

                             

                            function callnow(){

                             

                            return "Videosroll/Videos/1.flv";

                            }

                             

                            Action script:

                            import flash.Exteneral.ExternalInterface;

                            var videoURL:String=ExternalInterface.call("callnow");

                            tf.text=videoURL;

                            myVid.play(videoURL);

                             

                            HTML

                            <body onload="callnow()"></body>

                             

                            The textfield is dynamic instance name tf. When I run the code textfield goes undefined.

                            • 11. Re: Actionscript external interface onload
                              kglad CommunityMVP

                              the actionscript is calling the callnow function.   there's no need for that onload="callnow()".

                               

                              copy and paste the actionscript you're using.  if that is copied and pasted, you have a typo in you import statement that needs to be fixed.

                              • 12. Re: Actionscript external interface onload
                                Tony404 Community Member

                                Hi

                                 

                                Thanks for all the help. After some constant try and error I final got the code to work.  It was a different

                                external interface mark up but it worked fine.

                                 

                                 

                                Thanks.

                                • 13. Re: Actionscript external interface onload
                                  kglad CommunityMVP

                                  you're welcome.