ActiveDen

AS3: Accessing length of MP3

CreativeAddiction
CreativeAddiction Recent Posts
Threads Started
160 posts
  • Author had a Free File of the Month
  • Bought between 10 and 49 items
  • Europe
  • Exclusive Author
  • Has been a member for 2-3 years
  • Referred between 1 and 9 users
  • Sold between 1 000 and 5 000 dollars
CreativeAddiction says

Hey guys and gals,

Im currently coding an MP3 player for a project, i want to output a time display.

Currently i am waiting for the Event.COMPLETE in order to get the tracks length using

mySoundObj.length

but this means that whilst the MP3 is loading i do not have any length data to display.

I was wondering if there was a way to access tracks total length during the loading process?

Thanks!

-CA

CreativeAddiction
CreativeAddiction Recent Posts
Threads Started
160 posts
  • Author had a Free File of the Month
  • Bought between 10 and 49 items
  • Europe
  • Exclusive Author
  • Has been a member for 2-3 years
  • Referred between 1 and 9 users
  • Sold between 1 000 and 5 000 dollars
CreativeAddiction says

Hmmm, halfway there…

Kind of an estimated length.

//get Estimated length based on the bytes loaded and bytes total
var estMill:Number = (mySound.bytesTotal / (mySound.bytesLoaded / mySound.length));
var estMin:uint = Math.floor(estMill/60000);
var estSec:uint = (estMill%60000);
var estSecTen:uint = Math.floor(estSec/10000);
estSec = Math.ceil(estSec%10000)/1000;
var estLength:String = (estMin + ":" + estSecTen + "" + estSec).toString();

trace(estLength);

Still keen to hear if anyone knows a way of accessing the true length during the load process

:)

5013 posts The Dude Abides
  • United States
  • Elite Author
  • Has been a member for 4-5 years
  • Exclusive Author
  • Sold between 50 000 and 100 000 dollars
  • Contributed a Tutorial to a Tuts+ Site
  • Author had a Free File of the Month
+4 more
CodingJack says

I’ve banged my head against the wall on this many times and have come to the following conclusion:

It will never be 100% accurate until the mp3 has loaded completely so you just have to keep updating the text field using a calculation like above with an ENTER _FRAME until the mp3 has loaded completely.

2478 posts Premium Scripts, Plugins and Themes
  • Bought between 10 and 49 items
  • Elite Author
  • Exclusive Author
  • Has been a member for 6-7 years
  • Interviewed on the Envato Notes blog
  • Microlancer Beta Tester
  • Referred between 200 and 499 users
+1 more
LucidStudios says
Hmmm, halfway there…

Kind of an estimated length.

//get Estimated length based on the bytes loaded and bytes total
var estMill:Number = (mySound.bytesTotal / (mySound.bytesLoaded / mySound.length));
var estMin:uint = Math.floor(estMill/60000);
var estSec:uint = (estMill%60000);
var estSecTen:uint = Math.floor(estSec/10000);
estSec = Math.ceil(estSec%10000)/1000;
var estLength:String = (estMin + ":" + estSecTen + "" + estSec).toString();

trace(estLength);

Still keen to hear if anyone knows a way of accessing the true length during the load process

:)

no, there is no way to access the total length of the file so you always calculate the length of the downloaded portion.

1498 posts
  • Sold between 50 000 and 100 000 dollars
  • Helped protect Envato Marketplaces against copyright violations
  • Microlancer Beta Tester
  • Exclusive Author
  • Has been a member for 3-4 years
  • Europe
  • Referred between 10 and 49 users
  • Bought between 50 and 99 items
Firsh says
no, there is no way to access the total length of the file so you always calculate the length of the downloaded portion.

That is so lame. Not the workaround but that there is no way of telling the length before it is loaded. Other thing is that you can’t tell when the sound actually starts, I had to come up with a workaround for that (isBuffering check was a fail).

CreativeAddiction
CreativeAddiction Recent Posts
Threads Started
160 posts
  • Author had a Free File of the Month
  • Bought between 10 and 49 items
  • Europe
  • Exclusive Author
  • Has been a member for 2-3 years
  • Referred between 1 and 9 users
  • Sold between 1 000 and 5 000 dollars
CreativeAddiction says
no, there is no way to access the total length of the file so you always calculate the length of the downloaded portion.
That is so lame. Not the workaround but that there is no way of telling the length before it is loaded. Other thing is that you can’t tell when the sound actually starts, I had to come up with a workaround for that (isBuffering check was a fail).

My workaround for that was to check the channel.position, if greater than 0 then sound has started :)

Also, thanks to everyone for your advise, a shame that there is no way to get the length. I may use an XML variable for the length and use that during load and when Event.COMPLETE is called then use the actual .length property.

Thanks guys!

PS: if anyone wishes to use the code i posted, feel free ;)

1128 posts
  • Author had a File in an Envato Bundle
  • Has been a member for 4-5 years
  • Sold between 10 000 and 50 000 dollars
  • Exclusive Author
  • Referred between 10 and 49 users
  • Bought between 50 and 99 items
FlashTang says

Yeah ,seems estimating length is the only way

1498 posts
  • Sold between 50 000 and 100 000 dollars
  • Helped protect Envato Marketplaces against copyright violations
  • Microlancer Beta Tester
  • Exclusive Author
  • Has been a member for 3-4 years
  • Europe
  • Referred between 10 and 49 users
  • Bought between 50 and 99 items
Firsh says
My workaround for that was to check the channel.position, if greater than 0 then sound has started :)

I also came up with that exact solution :)

Back on topic: Flash could read the value that PHP got about the mp3:

With this PHP script it’s possible: http://getid3.org/. You can add that to the xml:

http://snipplr.com/view/43394/as-php-mp3-length/

CreativeAddiction
CreativeAddiction Recent Posts
Threads Started
160 posts
  • Author had a Free File of the Month
  • Bought between 10 and 49 items
  • Europe
  • Exclusive Author
  • Has been a member for 2-3 years
  • Referred between 1 and 9 users
  • Sold between 1 000 and 5 000 dollars
CreativeAddiction says
My workaround for that was to check the channel.position, if greater than 0 then sound has started :)
I also came up with that exact solution :)

Great minds… ;)

1498 posts
  • Sold between 50 000 and 100 000 dollars
  • Helped protect Envato Marketplaces against copyright violations
  • Microlancer Beta Tester
  • Exclusive Author
  • Has been a member for 3-4 years
  • Europe
  • Referred between 10 and 49 users
  • Bought between 50 and 99 items
Firsh says
Great minds… ;)

:) Check out my PHP solution. Tell me if you need help.

by
by
by
by
by