Calculate the length of a streaming mp3. Easy

Hi,
Flash is funny. :crazy:
When you stream an mp3 and try to get the duration of the song, you only get the duration of the part of the song that has been loaded thus far, but not the entire length of the song! This can be troublesome when making an mp3player. Actually if you ever try exporting (using the bandwidth simulator) the example code in the help files under “sound.duration”, its all messed up because the calculation for finding the sound played/the sound total is not using the time played/length of the entire song. But rather its using the time played/the length of the PART of the song that has been downloaded.

So how do we find out how long a song will play without downloading the entire thing first? EASY!

determineTime = function () {	
	soundInterval = setInterval(function () {
		var loaded = sound.getBytesLoaded();
		var duration = sound.duration;		
		kbps = ((loaded/1000)/(duration/1000));
                //trace(kbps);
		var total = sound.getBytesTotal()/1000;	
		timeTotal = (total/kbps)/60;
		//trace(timeTotal)
		clearInterval(soundInterval);		
	}, 4000);
};

So what happens here is totally simple. We first determine the kbps of the mp3. Then we use that to find the total length of the song.

Is it perfect? Nope. But good enough for me if I don’t have to specify length of every mp3 that I put into my player.

So there you go.

Hope someone finds that useful. I haven’t seen it before but who knows. Maybe it’s old news.

:ko:

oh yeah and check your publish settings if you keep getting 16kbs and that wasnt the sample rate of your mp3. Shouldn’t effect the calculation for the time though.