tags:

views:

119

answers:

2

hello,

is there a way to get mp3 song duration in JavaScript?

Thanks.

+2  A: 

That depends on what environment the JS is running in.

If you are in a browser which supports mp3 and the HTML5 audio API, then you can load the mp3 in an <audio> element and access the duration property via the DOM. If you are using some sort of Flash player, then it might expose that information using ExternalInterface.

There are other environments, but they aren't in such common usage.

David Dorward
not usimg a browser and not flash player either..
Gbolahan
@Gbolahan then I'm pretty sure it's impossible.
Pekka
Don't tell us what you're *not* using, tell us what you **are** using!
David Dorward
my bad..using appcelerator titanium JavaScript framework
Gbolahan
is it not possible to read mp3 tags and get it?
Gbolahan
Never looked at it before, but glancing over the website suggests that it runs in Safari, which would support `<audio>`
David Dorward
Yes, it's possible - if you want to write a parser in pure JS for the mp3 file format (or at least the subset that interests you).
David Dorward
+1  A: 

There is actually a library that can run client-side, attempting to fetch just enough of the MP3 to read the ID3 tags:

http://github.com/aadsm/JavaScript-ID3-Reader

Since you are using server-side JavaScript, you can directly read files from the server and do pretty much anything you want. If you can't find any libraries specifically for server-side use, I expect you can grab just the bits out of the above project that relate to parsing the ID3, and leave out the AJAX fetching stuff.

Other libraries are available with a Web search for Javascript ID3.

Nick