Did you know - Flex and ID3 tags

This post is more than 2 years old.

Ok, so this will elicit a "duh" out of my Flex 2 experienced readers, but I was pleasantly surprised to find that Flex 2 (to be clear, ActionScript 3) can read the ID3 tags out of MP3 files. I knew that I could play MP3s in Flex, but I didn't realize I could also dig out the ID3 information.

Like most things in Flex 2, it is incredibly easy. First you load a MP3:

sound = new Sound(new URLRequest("/music/80s/EURITHMIX-SWTDREMS.MP3"));

That's a relative URL there to an Apache virtual alias on my local server. Next you add an event listener:

sound.addEventListener(Event.ID3, onID3);

This basically says to run the onID3 function when the ID3 event fires. My demo onID3 method looked like so:

function onID3(event:Event):void { myid3info.text += sound.id3.songName + "\n"; myid3info.text += sound.id3.artist + "\n"; }

Where myid3info was a simple text control. Flex supports all the typical properties (album, artist, song name, etc). More information may be found at the Livedocs page.

I'd post a demo, but the last thing I need is a RIAA lawsuit. ;) FYI - I discovered this gem in the ActionScript 3.0 Cookbook.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Brian posted on 1/2/2007 at 8:11 PM

Note that (if I'm remembering correctly) ID3 information only works for runtime-loaded files. Embeds don't have the ID3 data.

Comment 2 by Raymond Camden posted on 1/2/2007 at 8:13 PM

Interesting. I wonder why it would NOT be supported. I guess it isn't a big deal since you are embedded a _known_ MP3.

Comment 3 by Daniel Todd posted on 1/2/2007 at 8:52 PM

Reading ID3 tags has actually been available for some time now, I think since one of the Flash 6 revisions. It's always nice to find ActionScript features you didn't quite expect to be there :)

Comment 4 by darron posted on 1/2/2007 at 9:19 PM

Lot's of good stuff in that there ActionScript 3.0 Cookbook! .. or so I hear ;-)

Comment 5 by Raymond Camden posted on 1/2/2007 at 9:26 PM

Hey Darron - I absolutely love it so far. I did notice one problem though. In 15.2 "Starting and Stopping a Sound", the text says:

The close() method of the Soutn object not only stops the sound from playing, but also stops the steeaming... The close() method should be called only when you are sure you are finished with that particular sound. Recipe 15.1 discusses a way to stop the sound from playing w/o stopping the stream.

However, in 15.1, there is no mention of this. The only mention of this is: "See Recipe 15.2 for information on how to control a sound's playing."

Seems like an infinite loop. ;)

Comment 6 by robert posted on 1/4/2007 at 12:55 AM

Just to let you know, Actionscript 2's sound object can also retrieve ID3 tags. It can read either ID3 1.0 or ID3 2.0. If your MP3 has ID3 2.0 tags, you can get just about any information out of the file you need. This functionality has been around for a while.

Comment 7 by Raymond Camden posted on 1/4/2007 at 1:00 AM

Flex is introducing me to all this new stuff in AS - new to me anyway. :)

Comment 8 by darron posted on 1/4/2007 at 2:04 AM

Hi Ray,

There isn't a direct way to stop a sound but still have it load in the background (i.e., there is no "pause" method on the Sound class).

However, to make sonud stop while still having the stream load you would write code like the following (pseudo-code, not tested)

// Save the sound channel after you tell the sound to play
var channel:SoundChannel = sound.play();

// Tell the channel to stop playing, but record the offset
// time so that you can resume playing later
var position:Number = sound.position;
channel.stop();

// Then, continue playing
sound.play( position );

The sound should continue to stream since only the channel was stopped and the sound was never closed.

Hope that helps...

Comment 9 by darron posted on 1/4/2007 at 2:08 AM

By the way, that refer to 15.1 looks like it was a typo, and should've actually sent you to 15.10 (oops!). The "Pausing and Restarting a Sound" covers the topic in a little more detail than my previous comment.

Comment 10 by Raymond Camden posted on 1/4/2007 at 2:10 AM

Thanks Darron!