In case you never used the Sound class before, let me give you a brief introduction.
The Sound class is found in the flash.media package and is used (hard not to be obvious here) for playing sounds. The constructor for the Sound class has 2 optional parameters:
- stream:URLRequest = new URLRequest(“url:String“);
- context:SoundLoaderContext = new SoundLoaderContext(“buffertime:Number” , “checkPolicyFile:Boolean” )
The SoundLoaderContext is crucial if you are loading sounds from another website (needed to prevent security errors). This class also allows you to determine the amount of milliseconds that flash will buffer before playing your file.
var $path:String = "assets/sounds/theme.mp3"; var $buffer:Number = 5000; var $checkPolicyFile:Boolean = false var s:Sound = new Sound(new URLRequest($path), new SoundLoaderContext($buffer, $checkPolicyFile));
So far, so good. We created a Sound object that is currently loading and will be ready to play when 5 seconds of audio have been buffered.
Now lets take a look at the Sound objects public properties. The ones that we are interested in are the following:
- length:Number = the number of milliseconds that have already been loaded and are ready to play
- bytesLoaded:uint = the number of bytes that are already loaded from the audio file
- bytesTotal:uint = the fileSize in bytes of the audio file
As you can see, one property that would be very useful is missing. Namely, the duration of the entire audio file in milliseconds (when fully loaded). Imagine that you would like to create an online music player, and you need to display a scrollbar to show the user to which part of the song he is currently listening (*) . If the file is still loading, you will be unable to use the length property, because it only shows the number of milliseconds that have already been loaded, not the entire length of the song ! Now have a look at this method:
private function guessDuration(s:Sound):Number { return Math.floor( s.length / (s.bytesLoaded / s.bytesTotal) ); }
This function will calculate a value for the total duration, based on the correlation that exists between bytesLoaded / bytesTotal and s.length / [s.length when downloading is finished] Please realize that this value remains an estimate, but for my applications, it has proven to be amazingly accurate. Happy Coding !
* Extra: getting the current position (in milliseconds) of a Sound when it’s playing.
So, now you know how to find an estimated value for the total playback time of your file. But if you are still planning on making the online music player with a scrollbar, you will also need to know at which point in the song you are when its playing. The Sound class doesn’t have a property called “position” or “currentPosition”, but luckily for us, the SoundChannel class does.
What’s important here, is that when you call the Sound.play() method, it returns a SoundChannel. If you keep this SoundChannel stored in a variable, you can later access its position:Number property, which returns the current position (in milliseconds) of you original Sound object.
... var sound:Sound = new Sound(new URLRequest("01.mp3")); var channel:SoundChannel = sound.play(); // now you can get the current position and the estimated length (both in milliseconds) var position:Number = channel.position; var duration:Number = guessDuration(sound);
Like this post ? Follow me and multimediacollege on Twitter !

[...] This post was mentioned on Twitter by Ultra Red, karannnnnnnnnnn3, fmchicago, FMC Washington DC, Jaaq Jorissen and others. Jaaq Jorissen said: Blogged: estimating the duration of a Sound Object while loading. #AS3 #actionscript http://bit.ly/cQgB7i [...]