Pausing swfs and audio in a browser when the tab is out of focus

I'm trying to code my flash file so that the html will pause all swfs AND audio when the tab is out of foucs.  I found this code on http://frontenddeveloper.net/wiki/index.php?title=JavaScript_and_VBScript_Injection_in_Act ionScript_3 and it works,but not completely. It only pauses the movie clips that are in the Flash file and not any that are exteranlly loaded with audio included.
How can I adjust it to pause the externally loaded swfs that are loaded to a mc within my main movie clip and the audio OR what should I use in place of this code?  Someone mentioned on a different post that I needed to use a window.onblur funcition, but they didn't give details.
import flash.display.MovieClip;
import flash.utils.setTimeout;
// This is a more "safe than sorry" setting, since multiple domains
// have entry into my site. Can be removed or hardcoded if you feel
// secure or insecure, as you see fit.
flash.system.Security.allowDomain("*");
// Throw any errors around to make sure somebody actually gets them.
ExternalInterface.marshallExceptions = true;
// This is not the most ideal way to toggle animations on and off, but
// it's thorough, generic, and simple. Iterate all movieclips within
// clip, shutting them down each in turn. A better, but much more tedious
// method would be to target specific clips using dotpath notation, telling
// each in turn to turn off or on depending on what we need.
// BUT this is just a demo, and what we're really interested in is the
// event-handling mechanism that actually calls this routine, and not the
// routine itself.
function toggleAllClips(doAnim, clip) {
if ( clip is MovieClip) {
  if (doAnim) {
   clip.play();
  } else {
   clip.stop();
  for (var i = 0; i<clip.numChildren; i++) {
   toggleAllClips(doAnim, clip.getChildAt(i));
function animOn(e:*=null) {
toggleAllClips(true, this.mainMC);
function animOff(e:*=null) {
toggleAllClips(false, this.mainMC);
function injectPrep(e:*=null) {
try {
  ExternalInterface.addCallback("jsanimOn", animOn);
  ExternalInterface.addCallback("jsanimOff", animOff);
} catch (e) {
  trace(e);
function injectListeners(e:*=null) {
try {
  // Object/Embed ID of this movie needs to be inserted into the
  // JavaScript before we actually wrap and send it to the browser:
  var jsfix=js.toString().replace(/xxx/g, ExternalInterface.objectID);
  ExternalInterface.call(jsfix);
} catch (e) {
  trace(e);
// Using timeouts ensures the movie is actually done loading before
// these fire, helping compatibility for a few browser versions.
setTimeout(injectPrep,0);
setTimeout(injectListeners,100);
JAVASCRIPTS
JavaScript needs to be wrapped in a tag, a cdata, and a closure
function in order to be wrapped up and sent to the browser.
Note that an ActionScript function will replace all instances
of "xxx" with the actual ID used for this SWF.
We're battling some major bugs and crossbrowser idiosyncrasies
here:
1) In Internet Explorer the 'onblur' event is implemented
    incorrectly (as opposed to Firefox/Mozilla browsers). It is
    wrongly fired when focus is switched between HTML elements
    *inside* a window. As a result, we have to use onfocusout
    instead of onblur, and keep track of which element is active.
    If we focusout and the active element is not the previous
    active element, then we haven't actually "blurred" and dont
    want to trigger Flash.
2) Firefox has problems interpreting both getElementById and
    document["swf"] when dealing with "twicebaked" object/embeds.
    Adobe's method of finding the swf fails to address the fact
    that document["swf"] sometimes returns an array in this
    situation rather than an object ref, and getElementById
    sometimes confuses name and id.
3) When a window is created in Firefox, it doesn't actually have
    "focus" (event though it appears to) and therefore won't "blur"
    unless you actually click in it first, i.e if you open up a
    window, then immediately send it to the background, it never
    gets the command to halt the flash. So we have to explicitly
    focus the blasted thing to get it to work properly.
4) Because of irregularities caused by Ajax, the way browsers shut
    down, and other factors, there's a good chance our swf won't
    be there when a blur (or focusout) event occurs. Therefore we
    need an explicit check within the event handler itself.
5) Finally, we want to wrap everything inside a wrapper-closure
    function, to keep everything safe from being stepped on. Lucky
    us, we have to do this anyways to get everything to fit inside
    a single ExternalInterface.call event.
var js:XML = <script><![CDATA[
( function() {
  var active_element; // tracker for ie fix;
  var bIsMSIE = false;
  // Modified version of Adobe's code resolves a bug in FF:
  function getSWF(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
      return window[movieName];
    } else {
      // Resolves a bug in FF where an array is sometimes returned instead of a
      // single object when using a nested Object/Embed.
      if(document[movieName].length != undefined){
        return document[movieName][1];
      return document[movieName];
  // Need to check for swf each time we try this because the swf may actually be gone
  // because of ajax or a window closure event. Prevents error dialog from popping up.
  // Future release should check for this condition and then remove the calling event
  // so it doesn't keep triggering.
  function animOff(){
    if (bIsMSIE && (active_element != document.activeElement)) {
      active_element = document.activeElement;
    } else {
      var logoThang = getSWF("xxx");
      if(logoThang){logoThang.jsanimOff();}
  function animOn(){
    if (bIsMSIE && (active_element != document.activeElement)) {
      active_element = document.activeElement;
    } else {
      var logoThang = getSWF("xxx");
      if(logoThang){logoThang.jsanimOn();}
  // Add the listeners. Hear ye, here ye.
  if (typeof window.addEventListener !== "undefined") {
    // Firefox, Mozilla, et al.
    window.addEventListener("blur", animOff, false);
    window.addEventListener("focus", animOn, false);
  } else if (typeof window.attachEvent !== "undefined") {
    // Internet Explorer
    bIsMSIE = true;
    window.attachEvent("onfocus", animOn);
// Another bug: window.onblur ALWAYS fires in IE, so
// we have to keep track of what we're clicking using
// another method:
active_element = document.activeElement;
document.attachEvent("onfocusout", animOff);
  // Necessary to trigger toggling in FF if the page hasn't actually been clicked in and the page
  // is sent to the background. Can be commented out if necessary, e.g. if you don't want the page
  // popping to the top or want focus to remain somewhere else like a form field.
  window.focus();
]]></script>;

I added this code and it removes the externally loaded swfs.  I don't want that, I want them to pause and then resume when the tab is back in foucs.  Also, the main code restarts the main movie clip upon refocusing too.
Added code:
function toggleAllLoaders(doAnim, loader) {
if ( loader is Loader) {
  if (doAnim) {
   loader.play();
  } else {
   loader.stop();
  for (var i = 0; i<loader.numChildren; i++) {
   toggleAllLoaders(doAnim, loader.getChildAt(i));
I added the new function to all of the places that had the "toggleAllClips" function.

Similar Messages

  • HT1583 When I render my project in IDVD, video and audio runs properly, but when I burn it in professional quality and burning is finished there was no audio on the last two chapters, one audio is attached to the clip and the other one is from itunes, pls

    Hi, please help me When I render my project in IDVD, video and audio runs properly, but when I burn it in professional quality and burning is finished there was no audio on the last two chapters, one audio is attached to the clip and the other one is from itunes, pls help

    Hi
    My first two thoughts
    • Shared to Media Browser - BUT as Large or HD - try Medium !
    • audio from iTunes - use to go silent
    - in iTunes
    - Create a new PlayList with needed audio
    - BURN this as a standard Audio-CD (.aiff) - NOT .mp3
    - use the files on this CD in Your Movie project - I ALWAYS DO - and only use .aiff ever - never .mp3'
    My notes on this:
    No audio on DVD disc.
    Most common origin. 
    1. Imported audio from iTunes.
    • In iTunes copy out as an Audio-CD .aiff (Same as played on standard Stereo CD - no .mp3)
    • Use this in Your movie project
    2. Low - Free Space on Start-up/Boot/Internal/Mac OS Hard disk makes it hard for iMovie to work as intended.
    Down to 1.3 GB and it doesn’t work - especially audio don’t migrate over to Media Browser
    (iM’08 & 09)
    3. Material in iMovie’08 & 09 - Shared to Media Browser and here selected as Large
    large.m4v. It silenced out this project in iDVD. By making a slight alteration - provoking it to ask for a new Share/Publish to Media Browser and here selecting 640x480 - and audio was back when a new iDVD project was created including this movie.
    Minuscular - - 176x144
    Mobile - - - - - 480x360
    Medium - - - - 640x480
    Large - - - - - - 720x540    960x540
    HD - - - - - - - - 1280x720
    4. Strange audio formats like .mp3, .avi etc.
    • Change them to .aiff. Use an audio editor like Audacity (free)
    5. Main audio is set to off in System Preferences - Does this by it self - Don’t know why
    Cheque Audio-Out resp. Audio-In
    6. Ed Hanna
    Had the same problem; some Googling around gave me a kludgy, but effective fix
    Downgrade Perian from recent versions back to version 1.0.
    That worked for me, and so far I haven't encountered any deficiencies — except it takes more advanced versions of Perian to enable QuickTime to handle subtitles in .srt format — that I have noticed.
    7. GarageBand fix.
    In this set audio to 44.1 kHz (Klaus1 suggestion)
    (if this don’t work try 48 kHz - me guessing)
    Before burning the DVD.
    • Do a DiskImage (File menu and down)
    • Double click on the .img file
    • Test it with Apple DVD-player
    If it’s OK then make Your DVD.
    Burn at x1 speed.... (or x4)
    • In iDVD 08 - or - 09
    • Burn from DiskImage with Apple’s Disk Utilities application
    • OR burn with Roxio Toast™ if You got it
    Yours Bengt W

  • Open PDF in same browser when the preference is set to open in new window.

    The corporate policy for all applications is to open the PDF document in new browser/reader but our application needs the reader to open in the same browser even the preference "Display PDF in Browser" is unchecked.... The setPreference from javascript (html) doesn't seemed to work... How do i make this work?
    Also, I need the links the browser to open in new browser once the PDF is loaded... do I need to reset the preference once it is loaded for the links to open in new browser?
    thanks,
    - Ray

    There is no way to override that from your application.   And if the Links in the PDF have an explicit "open in new window" setting, that is respected over the preference.
    From: Adobe Forums <[email protected]<mailto:[email protected]>>
    Reply-To: "[email protected]<mailto:[email protected]>" <[email protected]<mailto:[email protected]>>
    Date: Wed, 19 Oct 2011 22:02:53 -0700
    To: Leonard Rosenthol <[email protected]<mailto:[email protected]>>
    Subject: Open PDF in same browser when the preference is set to open in new window.
    Open PDF in same browser when the preference is set to open in new window.
    created by h_ray<http://forums.adobe.com/people/h_ray> in Acrobat SDK - View the full discussion<http://forums.adobe.com/message/3981167#3981167

  • [svn] 2692: Bug: BLZ-227 - When using JMS Destination, MessageClient and FlexClient not released from memory when the session times out .

    Revision: 2692
    Author: [email protected]
    Date: 2008-07-31 13:05:35 -0700 (Thu, 31 Jul 2008)
    Log Message:
    Bug: BLZ-227 - When using JMS Destination, MessageClient and FlexClient not released from memory when the session times out.
    QA: Yes
    Doc: No
    Checkintests: Pass
    Details: Fixed a memory leak with JMS adapter. Also a minor tweak to QA build file to not to start the server if the server is already running.
    Ticket Links:
    http://bugs.adobe.com/jira/browse/BLZ-227
    Modified Paths:
    blazeds/branches/3.0.x/modules/core/src/java/flex/messaging/services/messaging/adapters/J MSAdapter.java
    blazeds/branches/3.0.x/qa/build.xml

    Revision: 2692
    Author: [email protected]
    Date: 2008-07-31 13:05:35 -0700 (Thu, 31 Jul 2008)
    Log Message:
    Bug: BLZ-227 - When using JMS Destination, MessageClient and FlexClient not released from memory when the session times out.
    QA: Yes
    Doc: No
    Checkintests: Pass
    Details: Fixed a memory leak with JMS adapter. Also a minor tweak to QA build file to not to start the server if the server is already running.
    Ticket Links:
    http://bugs.adobe.com/jira/browse/BLZ-227
    Modified Paths:
    blazeds/branches/3.0.x/modules/core/src/java/flex/messaging/services/messaging/adapters/J MSAdapter.java
    blazeds/branches/3.0.x/qa/build.xml

  • Will the ipod 4th generation drop in price when the 5 comes out and if it does how much by

    will the ipod 4th generation drop in price when the 5 comes out and if it does how much by

    http://store.apple.com/us/browse/home/shop_ipod/family/ipod_touch/select_4thgene ration

  • Why is my ipod shuffle pausing my music when the screen times outs?

    My Ipod shuffle is pausing whenever the screen times out...please help.

    Make sure your headphones are plugged in all the way, meaning you cannot see any of the silver still showing from the headphone's plug. 
    A feature of the Nano is to put it to sleep when the display goes out and it does not detect any headphones.
    B-rock

  • My 4s continues to lose wi-fi and reverts to 3G even when the phone is 4 ft from the router.

    My 4s continues to lose wi-fi and reverts to 3G even when the phone is 4 ft from the router. Is this a defect in my phone? It happens when I am home and I switch to my wi-fi. It doesn't matter where the phone is in relation to the router. Please note, I have my phone set to NOT sleep. Does any one know why my phone reverts to 3G? I have to manually set it to wi-fi every time this happens.

    I have the same issue with my iPhone 4s. It was not an issue when I was using iOS5, but it started once I upgraded to iOS6 and the recent update of iOS6.0.1 did not fix it. Judging from the release notes, Apple thinks this is only an iPhone 5 issue, as they say they fixed it for iPhone 5's.
    I have an iPad 2 (iOS6.0.1) and a 3rd generation iPod touch (latest iOS5 release), as well as an Apple TV 2. None of the other devices have this problem and they are all on the same network.
    Something interesting... my iPhone 4s works fine on the wifi where I work. At home, it continually turns on and off. My home network is using a Netgear router (WNDR3400) and the iPhone is connecting to it using WPA2+PSK protocol. Hopefully, this information can help Apple figure out the problem.
    (I remember my iPod Touch having a similar issue a few years ago with iOS 4, when I had a different Netgear router using the same security protocol. I think iOS 5 fixed it for the iPod Touch... I wonder if some Apple products have an issue with Netgear Routers using that encryption.)

  • My sound won't work on my ipod 5. I plugged the speakers in and it works fine.  When the headphones aren't plugged in and I turn the volume up it says that they are plugged in.  Please help !

    my sound won't work on my ipod 5. I plugged the speakers in and it works fine.  When the headphones aren't plugged in and I turn the volume up it says that they are plugged in.  Please help !

    - Try cleaning out/blowing out the headphone jack. Try inserting/removing the plug a dozen times or so. (this was already sugested) There is a little switch in the headphone jack that disconnects the speaker when the headphone jack is inserted.
    Try the following to rule out a software problem
    - Check also Settings > General > Accessibility > Hearing. Make sure that the sound balance is not set all the way to "R".
    - Reset the iPod. Nothing will be lost
    Reset iPod touch: Hold down the On/Off button and the Home button at the same time for at
    least ten seconds, until the Apple logo appears.
    - Reset all settings
    Go to Settings > General > Reset and tap Reset All Settings.
    All your preferences and settings are reset. Information (such as contacts and calendars) and media (such as songs and videos) aren’t affected.
    - Restore from backup
    - Restore to factory settings/new iPod.
    - Make an appointment at the Genius Bar of an Apple store. Seems you have a bad headphone jack.
    Apple Retail Store - Genius Bar
    If not under warranty Apple will exchange your iPod for a refurbished one for this price. They do not fix yours.
    Apple - iPod Repair price                  
    A third-party place like the following will replace the jack for less. Google for more.
    iPhone Repair, Service & Parts: iPod Touch, iPad, MacBook Pro Screens
    Replace the jack yourself
    iPod Touch Repair – iFixit

  • Is it possible to insert a video and have it automatically play when the topic is opened?

    I am using Robohelp 10. Is it possible to insert a video and have it automatically play when the topic is opened?

    Hi Alchemex,
    What tool are you using to create the video.
    You can set preferences using the tool that you created the video with/
    For example, if you are using Adobe Captivate you can set videos to start automatically?
    Let me know if you need more detail.
    Kind Regards
    Craig

  • Will accountability software still pick up unsuitable browsing when the computer is in private browsing mode?

    Will accountability software still pick up unsuitable browsing when the computer is in private browsing mode?

    That depends. If they're reading Firefox files to see if you're visiting certain sites, then it won't show up. However if they're watching (or blocking) your network traffic or other things outside of Firefox, then they'll still be able to detect what sites you're visiting.

  • How do I insert an audio in html? When the reader access the widget it will be transferred to a page in Safari?

    How do I insert an audio in html?
    When the reader access the widget it will be transferred to a page in Safari?

    See iBooks Author: About HTML widget creation

  • My IPAD 4 is not working properly. it will blink and then shut down even when the battery is charged. i have done several reset on the iad. what do i do , i need help

    my IPAD 4 is not working properly. it will blink and then shut down even when the battery is charged. i have done several reset on the iad. what do i do , i need help

    iOS: Device unexpectedly restarts, displays Apple logo, or powers off
    http://support.apple.com/kb/TS5356
     Cheers, Tom

  • HT3529 When texting from my iphone 5, I was able to see when the text was delivered and when it was read.  An Apple Genius reset my iphone, and now I cannot see when the text was delivered or read.  Any suggestions?

    When sending a text, I was able to see when the text was delivered and when it was read, then an Apple Genius reset my phone and now I cannot see when the text was delivered or read.
    Any suggestions?

    Look in settings - message.

  • CS6 Illustrator and InDesign colours are changed when the PDF is saved.

    CS6 Illustrator and InDesign colours are changing when the saved PDFs are displayed on some different computers and for digital print output. I have ensured that the colours are all CMYK and tried different adjustments like preserving the numbers and the like but nothing is working. It prints properly on my inkjet and has displayed properly on different computers.

    You need to read up on color management, color profiles and all that....
    Mylenium

  • HT201302 my ipad is showing a sign for itunes and a connection. i cannot get anything else on my ipad, i was transfering photos to my computer when the ipad ran out of charge

    my ipad is just showing the itunes logo and a apple connection wire, wot to do, i cannot get anything else on my ipad. i was transferring photos to my computer when the ipad ran out of charge. please help.

    Sounds like it's gone into recovery mode : http://support.apple.com/kb/ht1808
    You will need to reset it via your computer's iTunes and you should then be able to restore/resync your content to it.

Maybe you are looking for