www.webdeveloper.com
Results 1 to 13 of 13

Thread: swfobject & stopping multiple instances of flash files

  1. #1
    Join Date
    Aug 2008
    Posts
    42

    swfobject & stopping multiple instances of flash files

    Hi Guys,

    I have a webpage where I have about 30 flash files that play audio previews loaded by swfobject.

    Is there a way that when I play one of the files I can stop any others that are playing on the page?

    Thanks

  2. #2
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    This would be dependant upon the javascript api (if any) provided by the swf files embedded in the page. Of course, my next thought is that there should be no reason to actually have 30 embedded flash files for this, why not just load a different file in the swf player for each one? Or have a playlist for it to pick from. There are, of course, a tremendous number of possibilities. What swf player are you using? What audio format (.mp3 hopefully)? I am familiar with (and use) JW Flv player, so I could help with that somewhat. For instance, rather than having 30 embeds, just one with no controls shown in the player (or the player not even shown), just some external controls, and a listing of available previews. Then could use the (JW Flv Player in this example) player's sendEvent function to load different files, or stop, pause or play, etc... As I say it's a pretty open-ended question which is dependant upon what you are using, and further integration needs as questioned by me. Perhaps a link to the page in question might be called for also..

  3. #3
    Join Date
    Aug 2008
    Posts
    42
    "astupidname", my page is a catalog.

    If you would be so kind as to see my site:

    www . foleysounds . com / browse . php

    You will see what I mean.

    Thanks

  4. #4
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    Ho ho ho, merry christmas to you!
    A three-part drop-in fix for you which should work out well enough for you (I have not tested with actual audio files yet, but should be just fine).
    I added a very slight interaction method within the stream1.swf file's actionscript, for it to be able to communicate with javascript, so the javascript can control turning previous player off when another one is clicked to play.
    In the stream1.swf file, I changed the actionscript for action layer frame 1 from:
    Code:
    playback.onRelease = function () {
        play ();
    };
    stop ();
    To the following:
    Code:
    import flash.external.ExternalInterface;
    function sendEvent(str) {
        switch(str) {
            case "Play":
                play();
                break;
            case "Stop":
                pausebutton.onRelease();
                break;
        }
    }
    firstRun = true;
    if (firstRun) {
        firstRun = false;
        id = id || ""; //if an id was passed in the flashvars one will be present, else id will be a blank string.
        //makes the 'sendEvent' function available to javascript:
        flash.external.ExternalInterface.addCallback("sendEvent", this, sendEvent);
    }
    playback.onRelease = function () {
        //makes a call to a javascript function named 'stream1Start', 
        //if one is provided, and passes this player's id and filePath flashvars to it:
        flash.external.ExternalInterface.call("stream1Start", id, filePath);
        play ();
    };
    stop ();
    So, with the above changes made to my copy of your stream1.swf file, you need to download the copy which is attached to the bottom of this post, in a .zip folder. Replace your existing one with it (after backing up the old copy - just in case, but then toss the old copy out as soon as you confirm my altered copy works right for you).
    Also, need to include the following javascript function on the page:
    Code:
    <script type="text/javascript">
    //this function will be called from within stream1.swf file when it is clicked to play, 
    //and it will pass the id (if one was provided in the flashvars) and filePath 
    //of the currently playing stream1.swf file into this function.
    //This function will stop the previously playing stream1.swf file, and allow the currently clicked one to play.
    //Note, we're not actually doing anything with the filePathReturnedFromFlash here, although we could have.
    //However, I set it up to use an id as an id is more correctly used as an identifier. Either way, really.
    function stream1Start(idReturnedFromFlash, filePathReturnedFromFlash) {
        var ac = arguments.callee, //this function it's self
            focusedPlayer = document.getElementById(idReturnedFromFlash); //the currently clicked (to play) player
        try {
            //if there is a currentlyActivePlayer property of this function 
            //(there won't be one the first time a player is clicked), 
            //and it is an object which has a sendEvent function available, this try will succeed:
            ac.currentlyActivePlayer.sendEvent("Stop"); //stops the currently playing stream1.swf file
        } catch(er){}
        ac.currentlyActivePlayer = focusedPlayer;
    }
    </script>
    Then, let's tidy up the ouput somewhat. Every place in the table td's where you have similar to:
    HTML Code:
    <span id="preview1935"><script language="javascript">swfobject.embedSWF("/stream1.swf", "preview1935", "60", "20", "9.0.0","/js/swfobject/expressInstall.swf", {filePath : "1935"}, {wmode:"transparent"}, false);</script></span>
    Change all those to just the span tags, remove the script's from all of them entirely so they become just this (with different id's in each, of course):
    HTML Code:
    <span id="preview1935"></span>
    Then, we'll embed the swf's in a loop instead of having all those extra scripts hanging around in the html where they don't belong. Note I added passing the object's id in the flashvars. Add the below javascript into the page, anywhere's below the link to swfobject in the head (suggest including the code within the tags below within the above script instead of having extra script tags floating around):
    Code:
    <script type="text/javascript">
    //just a throw-away, self-invoking function to wrap the var's and keep the global space clean:
    (function () {
        var idPrefix = 'preview';
        var idMin = 1906;
        var idMax = 1935;
        for (var i = idMin; i <= idMax; i++) { //>
            var id = idPrefix + i;
            swfobject.embedSWF("stream1.swf", id, "60", "20", "9.0.0", "/js/swfobject/expressInstall.swf", {filePath:i, id:id}, {wmode:"transparent"}, false);
        }
    })();
    </script>
    Let me know how that turns out for you, and enjoy!
    Attached Files Attached Files
    Last edited by astupidname; 12-25-2009 at 04:04 AM.

  5. #5
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    oops... just realized in the code I posted for the last javascript function, I forgot the slash in front of "stream1.swf", so don't forget to change that if necessary. Where I have: swfobject.embedSWF("stream1.swf",.....yada yada yada, should be: swfobject.embedSWF("/stream1.swf",...you get the picture....

  6. #6
    Join Date
    Aug 2008
    Posts
    42
    Merry christmas!!
    With a few tweaks got it just how i wanted it.
    Thanks very much!!!!

  7. #7
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    With a few tweaks got it just how i wanted it.
    Well... hmmm... the 'tweaks' you made were a pretty big deviation from what I showed, I'm wondering why? The changes you made do not work properly in FF or IE or most likely any others. The way you did it, it ONLY works if you play them in order from top to bottom, and does not work if you click around randomly on different ones, top to bottom or bottom to top. I just re-tested with my design and using sound files and the way I presented works flawlessly, cross-browser, so I would seriously recommend going back to more like what I had. The addition of the 'sources' array is just fine (actually, a good catch on your part, I had not noticed that things were not actually in numeric order before), in the event that there could ever be any 'holes' in the numeric sequence (or in this case, where the sequence is not ordered), but the sources array should only be used for the embedding, not the turning off. Note that what I showed is also more efficient when it comes to stopping the currently playing file, there is no need for a loop to attempt to shut off the others, when there should only ever be one to turn off. I'm imagining you probably have not noticed the flaw in your implementation yet, but if you test more by clicking around in a random fashion you will see the problem.
    Just thought I'd let you know about the problem, hope you get it ironed out, and good luck.
    Last edited by astupidname; 12-25-2009 at 05:31 PM.

  8. #8
    Join Date
    Aug 2008
    Posts
    42
    Funny...for me it was the other way around. Your version wasn't working for me.

    But I played mine out of sequence and it worked just fine. I will test it on a few browsers.

    P.S. That's really scary that you can open up flash files and see their contents.

  9. #9
    Join Date
    Aug 2008
    Posts
    42
    And. P.S. - I thought flash was cross browser - was it the flash or js that wasn't working??

  10. #10
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    And. P.S. - I thought flash was cross browser - was it the flash or js that wasn't working??
    Yeah, it is for the most part -far as I know (I'm not by any means a real pro at flash, just been doing quite a bit of it lately -delving in to AS3 actually more so)- cross-browser, although there may be cross-js-to-flash differences in places, not sure. It's the turning off of the previously playing file that is not working right, I'm imagining that you could maybe alter your 'stopOthers' function to be more like my 'stream1Start' function and it may be just fine. Although you may need to revert the actionscript also.
    P.S. That's really scary that you can open up flash files and see their contents.
    I'm doing that with So-Think SWFQuicker, just FYI in case you wondered. And, actually, I don't think it's scary, it is how it should be, IMO, but that's just me.

  11. #11
    Join Date
    Aug 2008
    Posts
    42
    Can you resend as a flash project? I'm on a mac - I can't download swfquicker

  12. #12
    Join Date
    Aug 2008
    Posts
    42
    Quote Originally Posted by astupidname View Post
    Well... hmmm... the 'tweaks' you made were a pretty big deviation from what I showed, I'm wondering why? The changes you made do not work properly in FF or IE or most likely any others. The way you did it, it ONLY works if you play them in order from top to bottom, and does not work if you click around randomly on different ones, top to bottom or bottom to top. I just re-tested with my design and using sound files and the way I presented works flawlessly, cross-browser, so I would seriously recommend going back to more like what I had. The addition of the 'sources' array is just fine (actually, a good catch on your part, I had not noticed that things were not actually in numeric order before), in the event that there could ever be any 'holes' in the numeric sequence (or in this case, where the sequence is not ordered), but the sources array should only be used for the embedding, not the turning off. Note that what I showed is also more efficient when it comes to stopping the currently playing file, there is no need for a loop to attempt to shut off the others, when there should only ever be one to turn off. I'm imagining you probably have not noticed the flaw in your implementation yet, but if you test more by clicking around in a random fashion you will see the problem.
    Just thought I'd let you know about the problem, hope you get it ironed out, and good luck.
    The script you wrote just doesn't work in safari...I an thrown for a loop...

  13. #13
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    I just checked your page (and the recent changes you made to the javascript & swf -much closer now to what I had shown, but with a few small 'tweaks' by you) and it looks to be working properly now, in IE 7, 8, FF 3.5 & Safari 4.0 (Windows), so congrats!! You're not still having trouble with Safari on Mac at all, are you? If not, glad you got it worked out!

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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