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
This commit is contained in:
Ray Greenwell
2008-11-17 16:14:07 +00:00
parent 526db3bb05
commit 5bc77aa0d0
@@ -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));
}