Flex web application: prevent framerate drop when window is invisible

1.1k Views Asked by At

So there's been a new "feature" in the flash player since version 10.1, which reduces the player's framerate to 2 fps when the application window is out of view. This is good news for performance, but it can break some functionality, such as the Timer class.

I have an application which uses a Timer to display a countdown. Given the nature of the application, it is required for the Timer to complete its countdown even if the user is not there to see it. Imagine that you need to give the user only 10 seconds to perform a task. If the user minimizes the window halfway through the counter, they can take as much time as they want and still have 5 seconds left when they return to the window. This apparently can not be avoided with the newer flash players.

In Air applications there is the backgroundFrameRate property which can be set to prevent this behavior, but this is part of the WindowedApplication class, so it seems that it is not available in a web application. Does anyone know a way to keep a constant frame rate even when the window is not visible? Thanks

3

There are 3 best solutions below

0
On

Testing with:

private var numer:int = 0;
private var prevNumer:int = 0;
private var timer:Timer = new Timer( 1000, 0 )

[...]

var tf:TextField = new TextField ();
addChild (tf);
addEventListener ( Event.ENTER_FRAME, onEnterFrame )
timer.addEventListener (TimerEvent.TIMER, onTimer )
timer.start()
function onTimer ( e:TimerEvent ):void
{ tf.appendText (' ' + (numer - prevNumer)); prevNumer = numer;}
function onEnterFrame ( e:Event ):void { numer++ }

shows clearly, that when You see the flash, tf appends numbers equal to Your FPS. If timer would get changed together with FPS, You wouldn't see a difference when minimizing a window. But, coming back You see 2 2 2 2 2, that is, FPS dropped to 2.

onDeactivate solution by AsTheWormTurns doesn't work. Event is fired, but fps not changed. wmode=opaque solution by Mr Brian Bishop doesn't work too

something obvious to try: change onEnterFrame function to set FPS:

function onEnterFrame ( e:Event ):void { numer++; stage.frameRate = 30 }

Obviously You can't set FPS when flash is not visible! Well, You can't set FPS unless You set it to 1.

Workaround to Your problem is simple, just make another timer similar to this above but with additional conditional:

function onTimer ( e:TimerEvent ):void {
if ( numer - prevNumer == 2 ) adjustOriginalTimer();
tf.appendText (' ' + (numer - prevNumer)); prevNumer = numer;
}

E: You can read about it here: http://help.adobe.com/en_US/as3/mobile/WS4bebcd66a74275c36cfb8137124318eebc6-8000.html

0
On

Setting the wmode parameter of the embedded swf to opaque will prevent the framerate throttling.

Brian

2
On

I've not tried myself, but maybe you can try to force the framerate onDeactivate:

stage.addEventListener(Event.DEACTIVATE, onDeactivate); 

function onDeactivate (e:Event):void 
{ 
    //eg myFrameRate=24
    stage.frameRate = myFrameRate; 
} 

Let me know if this works.