chchrist
chchrist

Reputation: 19802

AS3 - How can I find the total time of an mp3 before is downloaded?

How can I find the total time of an mp3 before it is fully downloaded? I have this function which is called on TimerEvent.TIMER

private function onTick(e:TimerEvent):void
 {
  _soundLength = _sound.length;
  _position = _channel.position;
  mp3Interface.timeBar.timers.elapsedTime.text = ascb.util.DateFormat.formatMilliseconds(_position);
  mp3Interface.timeBar.timers.totalTime.text = ascb.util.DateFormat.formatMilliseconds(_soundLength);
  var percentPlayed:Number = Math.round((_position/_soundLength)*100);
  mp3Interface.timeBar.seeker.x = (percentPlayed*mp3Interface.timeBar.progressBar.width-5)/100;
 }

The problem is tha the totalTime is correct only when the mp3 is fully downloaded..

Upvotes: 0

Views: 1664

Answers (2)

Corneliu Dascălu
Corneliu Dascălu

Reputation: 3958

The easiest way to do this is to estimate the length of the mp3 file, based on the total file size and the loaded bytes so far:

//the loading progress handler
private function progressHandler(e:ProgressEvent){
   _soundLength = _sound.length*e.bytesTotal/e.bytesLoaded;
}

It gives a pretty good estimate. The _soundLength property may vary by 1 or 2 seconds until loading is complete. Anyway, it's the only way to figure out the length of any mp3. Of course, if your mp3 files have ID3 info, you can get the length of the mp3 from there, but not all mp3s do.

Upvotes: 1

George Profenza
George Profenza

Reputation: 51837

Have a look at the mp3infoutil library.

Upvotes: 2

Related Questions