From 5bc77aa0d0a35471955acd42ff8a07c4eb6d6c3c Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Mon, 17 Nov 2008 16:14:07 +0000 Subject: [PATCH] Possible type 3 optimization, but well... it doesn't really make the code weirder, but it does add a bit more code. ( http://smallwig.blogspot.com/2008/04/smallwig-theory-of-optimization.html mdb pointed this post out to me.) It just bugged me that we use this player to play background music in whirled, and in that circumstance 99% of the time nobody cares about these events, so why have a timer to calculate and fire them off? git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@712 ed5b42cb-e716-0410-a449-f6a68f950b19 --- .../threerings/flash/media/Mp3AudioPlayer.as | 48 +++++++++++++++---- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/src/as/com/threerings/flash/media/Mp3AudioPlayer.as b/src/as/com/threerings/flash/media/Mp3AudioPlayer.as index a87f7fae..0269a8a0 100644 --- a/src/as/com/threerings/flash/media/Mp3AudioPlayer.as +++ b/src/as/com/threerings/flash/media/Mp3AudioPlayer.as @@ -142,10 +142,46 @@ public class Mp3AudioPlayer extends EventDispatcher // ignore } } - _positionChecker.reset(); + _state = MediaPlayerCodes.STATE_UNREADY; + checkNeedTimer(); _lastPosition = NaN; } + override public function addEventListener ( + type :String, listener :Function, useCapture :Boolean = false, + priority :int = 0, useWeakReference :Boolean = false) :void + { + super.addEventListener(type, listener, useCapture, priority, useWeakReference); + + if (type == MediaPlayerCodes.POSITION) { + checkNeedTimer(); + } + } + + override public function removeEventListener ( + type :String, listener :Function, useCapture :Boolean = false) :void + { + super.removeEventListener(type, listener, useCapture); + + if (type == MediaPlayerCodes.POSITION) { + checkNeedTimer(); + } + } + + protected function checkNeedTimer () :void + { + const needTimer :Boolean = (_state == MediaPlayerCodes.STATE_PLAYING) && + hasEventListener(MediaPlayerCodes.POSITION); + const isRunning :Boolean = _positionChecker.running; + if (needTimer != isRunning) { + if (needTimer) { + _positionChecker.start(); + } else { + _positionChecker.reset(); + } + } + } + /** * Play without updating the current state. */ @@ -185,15 +221,7 @@ public class Mp3AudioPlayer extends EventDispatcher protected function updateState (newState :int) :void { _state = newState; - - if (_state == MediaPlayerCodes.STATE_PLAYING) { - _positionChecker.start(); - - } else { - _positionChecker.reset(); - // pause() ends up checking the position one last time before dispatching state.. - } - + checkNeedTimer(); dispatchEvent(new ValueEvent(MediaPlayerCodes.STATE, newState)); }