From 858467870158f488c0648d651f8b6da153863d2e Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Wed, 19 Nov 2008 05:19:38 +0000 Subject: [PATCH] Player BAM5 told me about code his friend Shango wrote, for extrapolating the total duration of an mp3 while it was loading. Sure enough: we have length (so far), bytesLoaded and bytesTotal. So: do that. It works pretty well! Too bad we can't do something similar for FLVs that don't have metadata. :( (there is no "current length" property, just a current position. So any time estimate would get worse and worse as the bytes advanced past the play position. git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@719 ed5b42cb-e716-0410-a449-f6a68f950b19 --- src/as/com/threerings/flash/media/Mp3AudioPlayer.as | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/as/com/threerings/flash/media/Mp3AudioPlayer.as b/src/as/com/threerings/flash/media/Mp3AudioPlayer.as index 0c8b5582..d0e08f59 100644 --- a/src/as/com/threerings/flash/media/Mp3AudioPlayer.as +++ b/src/as/com/threerings/flash/media/Mp3AudioPlayer.as @@ -77,7 +77,12 @@ public class Mp3AudioPlayer extends EventDispatcher if (_sound == null) { return NaN; } - return _sound.length / 1000; + const duration :Number = _sound.length / 1000; + if (_isComplete) { + return duration; + } + // extrapolate the total duration based on the current length and current bytesLoaded + return (duration / _sound.bytesLoaded) * _sound.bytesTotal; } // from AudioPlayer @@ -233,8 +238,8 @@ public class Mp3AudioPlayer extends EventDispatcher */ protected function handleLoadingComplete (event :Event) :void { - handlePositionCheck(); // also updates duration _isComplete = true; + dispatchEvent(new ValueEvent(MediaPlayerCodes.DURATION, getDuration())); } protected function handlePlaybackComplete (event :Event) :void