www.webdeveloper.com
Results 1 to 4 of 4

Thread: Interacting With Flash Object via PHP/Javascript/Ajax

  1. #1
    Join Date
    Dec 2010
    Posts
    232

    Interacting With Flash Object via PHP/Javascript/Ajax

    Within my site I have the following code:-

    Code:
    <form>
        <button onClick="changeVideo(2)" type="button"/>
    </form>
    
    <object id="videoPlayer" type="application/x-shockwave-flash" data="http://www.mysite.com/videoplayer.swf">
        <param name="movie" value="http://www.mysite.com/videoplayer.swf" />
        <param name="allowscriptaccess" value="always" /> 
        <param name="FlashVars" value="currentVideo=1" />
    </object>
    By default when my site loads the flash file gets the value of currentItem from Flashvars and does what it needs to do with this value. The flash file in question is just a basic video player and the id of the video to be played is retrieved from flashvars.

    However I want users to now be able to click the button in my form and then the value of flashvars in my videoPlayer object will change to the value passed in the changeVideo() function i.e. in the above example value="currentVideo=1" will become value="currentVideo=2".

    That's the easy part I think but what I then need to do is get the flash file itself to load this video without a page refresh but I have no idea how to get the flash file to recognise this value once it has already loaded. What exactly are my best options here?:-

    1. Reload the flash component via ajax every time a user selects a new video i.e. every time changeVideo() is fired?

    2. Something else?

  2. #2
    Join Date
    Dec 2010
    Posts
    232
    Since my last post I've looked into external interface to allow communication between swf and html. Here is the code I've got so far:-

    Code:
    <script type="text/javascript">
    
    function receiveTextFromAS3(Txt) {
    document.getElementById('htmlText').value = Txt;
    }
    function sendTextToAS3(){
    var Txt = document.getElementById('htmlText').value;
    var flash = document.getElementById("as3_js");
    flash.sendTextFromJS(Txt);
    document.getElementById('htmlText').value = "";
    }
    
    </script>
    
    </head>
    
    <body>
    
    
    <textarea name="htmlText" id="htmlText" cols="50" rows="15"> </textarea>
        <input type="button" name="sendToFlash" id="sendToFlash" value="Send Text To Flash" onclick="sendTextToAS3();" />
    
    <div id="alt">
    
            <object id="as3_js" style="width: 100%;" type="application/x-shockwave-flash" data="http://www.mysite.com/videoplayer.swf">
            <param name="movie" value="http://www.mysite.com/videoplayer.swf" />
            <param name="allowscriptaccess" value="always" /> 
            <param name="FlashVars" value="" />
            </object>
    
    </div>
    The problem is when I click the button nothing happens and I get the JS error:-


    Uncaught TypeError: Object #<HTMLObjectElement> has no method 'sendTextFromJS'

    Can anyone suggest anything to fix this?

  3. #3
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    I suppose you should use some ActionScript (which is the Flash "cousin" of JavaScript) methods, written in your Flash file, in order to launch a JavaScript function.

    sendTextFromJS(), as far as I know, works only if you have defined the ActionScript method ExternalInterface.addCallback() within the SWF file.
    Last edited by Kor; 10-01-2011 at 05:41 AM.

  4. #4
    Join Date
    Dec 2010
    Posts
    232
    Thanks for the suggestion Kor. Strangely I moved the javascript after my after my video object and now it works fine so I assume it was something to do with the video file not being loaded yet.

    A bit off topic but on the subject of Actionscript would you happen to know of any tutorials for how to create a 'scrubber' for a video in flash or for a sound file/channel in flash? I can't seem to find anything decent related to that anywhere besides plugins which are a no go for me as it has to be self produced.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles